UVa:755 - 487--3279

来源:互联网 发布:sql语句的执行顺序 编辑:程序博客网 时间:2024/05/20 03:05

题意:找出有重复的电话号码,输出这些号码的标准形式和出现的次数。

当电话号码不足7位时,用‘0’补位。

刚开始以为用边输入边比较,最后排序输出是最高效的算法,但一直Time limit exceeded,“山外青山楼外楼”,网友们的算法更是巧妙和高效啊,涨姿势了。

发现一点:用sort函数无法对二维字符数组进行排序,只能将二维数组转换成结构体数组,再对结构体数组进行排序。

Code:

#include<stdio.h>#include<string.h>#include<ctype.h>#include<algorithm>using namespace std;struct DS{    char pn[300];};DS ds[100010];char mapn[30]="22233344455566670778889990",s[300];bool cmp(DS a,DS b){    return strcmp(a.pn,b.pn)<0;}int main(){    int N;    scanf("%d",&N);    while(N--){        int n;        scanf("%d",&n);        getchar();        for(int i=0;i<n;i++){            int t=0;            char ch;            memset(s,0,sizeof(s));            while(ch=getchar()){                if(ch!='\n'){                    if(isalpha(ch)){                        int e=ch-'A';                        s[t++]=mapn[e];                    }                    if(isdigit(ch))                        s[t++]=ch;                }                else                    break;            }            if(t<7){                for(int j=t;j<7;j++)                    s[j]='0';            }            s[7]='\0';            strcpy(ds[i].pn,s);        }        sort(ds,ds+n,cmp);        int num=0,p=0,j=0;        for(int i=0;i<n;i++){            if(strcmp(ds[j].pn,ds[i].pn)==0)                num++;            else{                if(num>1){                    p=1;                    for(int k=0;k<7;k++){                        printf("%c",ds[j].pn[k]);                        if(k==2)                            printf("-");                    }                    printf(" %d\n",num);                }                j=i;                num=1;            }            if(i==n-1&&num>1){                p=1;                for(int k=0;k<7;k++){                    printf("%c",ds[j].pn[k]);                    if(k==2)                        printf("-");                }                printf(" %d\n",num);            }        }        if(!p)            printf("No duplicates.\n");        if(N) printf("\n");    }    return 0;}

--------------------------------------------------------------------------------------------

          Keep It Simple,Stupid!

--------------------------------------------------------------------------------------------
0 0
原创粉丝点击