您的位置:首页 > 其它

poj 1002 487-3279

2010-09-14 21:32 344 查看
http://acm.pku.edu.cn/JudgeOnline/problem?id=1002

自己琢磨出用qsort排字符串数组,虽然时间很多860ms 但是很有成就感

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void*a,const void*b){
return strcmp((char*)a,(char *)b);
}
int main(){
char s[100001][10],ch;
int i,j,num,flag,flag1;
int n;
while(scanf("%d",&n)!=EOF){
j=0;
getchar();
for(i=0;i<n;i++){
ch=getchar();
while(ch!='/n'){
switch(ch){
case 'A':
case 'B':
case 'C': ch='2';break;
case 'D':
case 'E':
case 'F': ch='3';break;
case 'G':
case 'H':
case 'I': ch='4';break;
case 'J':
case 'K':
case 'L': ch='5';break;
case 'M':
case 'N':
case 'O': ch='6';break;
case 'P':
case 'R':
case 'S': ch='7';break;
case 'T':
case 'U':
case 'V': ch='8';break;
case 'W':
case 'X':
case 'Y': ch='9';break;
}
if(ch>='0'&&ch<='9'){
if(j==3){s[i][j]='-';j++;}
s[i][j]=ch;
j++;
}
ch=getchar();
}
s[i][j]='/0';
j=0;
}
flag=flag1=0;
qsort(s,n,sizeof(s[0]),cmp);
flag=0;num=1;
for(i=0;i<n-1;i++){
for(j=i+1;j,n;j++){
if(strcmp(s[i],s[j])==0){
i=j;
flag=1;
flag1=1;
num++;
}
else break;
}
if(flag){printf("%s %d/n",s[i],num);flag=0;num=1;}
}
if(!flag1) printf("No duplicates./n");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: