Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:各位,我是新手,请指教:用的堆排,为什么总是超时……该怎么改,难道非用快排不可么

Posted by zwjowen at 2010-04-12 16:02:38 on Problem 1002
In Reply To:各位,我是新手,请指教:用的堆排,为什么总是超时……该怎么改,难道非用快排不可么 Posted by:lhyan792 at 2010-04-02 12:35:12
操作字符串,岂不是很麻烦,还不如把它变成整数,用数组直接统计,看哥的代码,超级简单,排序简直是一件无聊的事情,只需数组weight[10000000]就可以了
#include<iostream>
#include<string.h>
using namespace std;
char str[100];
int len;
int REFER[26]={2,2,2, 3,3,3, 4,4,4, 5,5,5, 6,6,6, 7,0,7,7, 8,8,8, 9,9,9,0 };
int weight[10000000];
int POW[7]={1000000,100000,10000,1000,100,10,1};
char out[9];
int main()
{
	memset(weight,0,sizeof(weight));
	int n;
	scanf("%d",&n);
	int num,index;
//将字符串转化成整数,直接定位统计
	for(int mem=0;mem<n;mem++){
		scanf("%s",str);
		len=strlen(str);
		num=0;
		index=0;
		for(int i=0;i<len;i++){
			if(str[i]>='0' && str[i]<='9')
				num+=(str[i]-'0')*POW[index++];
			else if(str[i]>='A' && str[i]<='Z')
				num+=(REFER[int(str[i]-'A')])*POW[index++];
		}
		weight[num]++;
	}
//将整数转化成合适的输出形式输出
	int xx=0;
	out[8]='\0';
	for(int i=0;i<10000000;i++){
		if(weight[i]>=2){
			for(int j=0;j<8;j++)
				out[j]='0';
			out[3]='-';
			int temp=i;
			int k=7;
			while(temp>0){
				if(k==3)
					k--;
				out[k]='0'+(temp-(temp/10)*10);
				temp/=10;
				k--;
			}
			printf("%s %d\n",out,weight[i]);
			xx++;
		}
	}
	if(xx==0)
		printf("No duplicates.\n");
	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator