UVA 755 - 487--3279

来源:互联网 发布:st单片机代理 编辑:程序博客网 时间:2024/06/10 09:25

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.


The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:


A, B, and C map to 2

D, E, and F map to 3

G, H, and I map to 4

J, K, and L map to 5

M, N, and O map to 6

P, R, and S map to 7

T, U, and V map to 8

W, X, and Y map to 9


There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.


Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)


Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.


Input

The first line of the input contains the number of datasets in the input. A blank line follows. The first line of each dataset specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. There's a blank line between datasets.


Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.

Print a blank line between datasets.


Sample Input

1124873279ITS-EASY888-45673-10-10-10888-GLOPTUT-GLOP967-11-11310-GINOF101010888-1200-4-8-7-3-2-7-9-487-3279

Sample Output

310-1010 2487-3279 4888-4567 3

题目大意是先处理若干字符串成标准电话号码格式,若有重复的则排序输出,无则输出"No duplicates."


贴代码,注释部分是开始做的,是先O(n^2)找出重复的,再挑出来进行排序,于是TLE了。后来改用直接先将所有字符串排序,再数重复数,这样只用O(n)的复杂度。时限3s,用时600ms。

#include<functional>#include<algorithm>#include<iostream>#include<fstream>#include<sstream>#include<iomanip>#include<numeric>#include<cstring>#include<climits>#include<cassert>#include<cstdio>#include<string>#include<vector>#include<bitset>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<list>#include<set>#include<map>using namespace std;int getnum()//读大量数据特别快{    char ch;    while(ch=getchar(),ch==10||ch==32);    int ans=ch-48;    while((ch=getchar())!=EOF&&(ch>='0'&&ch<='9'))    {        ans*=10;        ans+=ch-'0';    }    return ans;}char a[100010][100],b[100010][100];int q[100010];struct info{    char z[100];    int num;}zz[100010];int cmp(const void *a,const void *b){    return strcmp((char *)a,(char *)b);}int main(int argc,char *argv[]){    int t,n,cnt;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<=n-1;i++) scanf("%s",a[i]);//其实可以读一个处理一个节省空间        for(int i=0;i<=100009;i++) q[i]=1;        for(int i=0;i<=n-1;i++)        {            cnt=0;            for(int j=0;j<=strlen(a[i])-1;j++)            {                if(isdigit(a[i][j])||isalpha(a[i][j]))                {                    if(isdigit(a[i][j])) b[i][cnt++]=a[i][j];                    else                    {                        switch(a[i][j])                        {                                case 'A':case 'B':case 'C': b[i][cnt++]='2';break;                            case 'D':case 'E':case 'F': b[i][cnt++]='3';break;                            case 'G':case 'H':case 'I': b[i][cnt++]='4';break;                            case 'J':case 'K':case 'L': b[i][cnt++]='5';break;                            case 'M':case 'N':case 'O': b[i][cnt++]='6';break;                            case 'P':case 'R':case 'S': b[i][cnt++]='7';break;                            case 'T':case 'U':case 'V': b[i][cnt++]='8';break;                            case 'W':case 'X':case 'Y': b[i][cnt++]='9';break;                        }                    }                }            }            b[i][cnt]='\0';        }        /*for(int i=0;i<=n-1;i++)        {            if(q[i]==-1) continue;            for(int j=i+1;j<=n-1;j++)            {                if(q[j]==-1) continue;                if(strcmp(b[i],b[j])==0)                {                    q[i]++;q[j]=-1;                }            }        }        int cnt=0;        for(int i=0;i<=n-1;i++)        {            if(q[i]!=-1&&q[i]!=1)            {                strcpy(zz[cnt].z,b[i]);                zz[cnt].num=q[i];                cnt++;            }        }        if(cnt==0) printf("No duplicates.\n");        else        {            //sort(zz,zz+cnt,cmp);            qsort(zz,cnt,sizeof(zz[0]),cmp1);            for(int i=0;i<=cnt-1;i++)            {                printf("%c%c%c-%c%c%c%c %d\n",zz[i].z[0],zz[i].z[1],zz[i].z[2],zz[i].z[3],zz[i].z[4],zz[i].z[5],zz[i].z[6],zz[i].num);            }        }        printf("\n");*/        qsort(b,n,sizeof(b[0]),cmp);        strcpy(zz[cnt].z,"A");        int count=1,ok=1;        for(int i=0;i<=n-1;i++)            if(!strcmp(b[i],b[i+1]))                count++;            else            {                if(count>1)                {                    printf("%c%c%c-%c%c%c%c %d\n",b[i][0],b[i][1],b[i][2],b[i][3],b[i][4],b[i][5],b[i][6],count);                     ok=0;                }                count=1;            }        if(ok)            printf("No duplicates.\n");        if(t)            printf("\n");    }    return 0;}
值得收藏。


还有一种做法是将处理后的字符串作为数字存储在大数组里,这样省去了排序的时间,用空间换取时间的典型!

#include<functional>#include<algorithm>#include<iostream>#include<fstream>#include<sstream>#include<iomanip>#include<numeric>#include<cstring>#include<climits>#include<cassert>#include<cstdio>#include<string>#include<vector>#include<bitset>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<list>#include<set>#include<map>using namespace std;int trans(char c){    if(c=='A'||c=='B'||c=='C') return 2;    if(c=='D'||c=='E'||c=='F') return 3;    if(c=='G'||c=='H'||c=='I') return 4;    if(c=='J'||c=='K'||c=='L') return 5;    if(c=='M'||c=='N'||c=='O') return 6;    if(c=='P'||c=='R'||c=='S') return 7;    if(c=='T'||c=='U'||c=='V') return 8;    if(c=='W'||c=='X'||c=='Y') return 9;    return c-'0';}int a[10000000];int main(){    int N,num;    scanf("%d",&N);    string str;    while(N--)    {        num=0;        cin>>str;        for(int i=0;i<str.length();i++)        {            if(str.at(i)!='-')             num=num*10+trans(str.at(i));        }        a[num]++;    }    num=0;    for(int i=0;i<10000000;i++)    {        if(a[i]>1)        {            num=1;             printf("%03d-%04d %d\n",i/10000,i%10000,a[i]);        }    }    if(num==0)        printf("No duplicates.\n");    return 0;}

同样值得收藏与反复咀嚼。

0 0
原创粉丝点击