A - 487-3279

Description

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 input will consist of one case. The first line of the input 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.       
 
 

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.        
 
 

Sample Input

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279

Sample Output

310-1010 2
487-3279 4
888-4567 3

以下为超时代码,太繁琐了!!!
#include <iostream>
#include <string>
using namespace std;
int f(char s[][10],int x[],int y[],int n)
{
    int q=0,t,p,i,k,j;
    for(i=0;i<n-1;i++){
        if(s[i][0]=='*')continue;
        y[q]=1;
        for(k=i+1;k<n;k++){
            bool flag=true;
            for(j=0;j<7;j++){
                if(s[i][j]!=s[k][j]){
                    flag=false;
                    break;
                }
            }
            if(flag==true){
            s[k][0]='*';
            y[q]++;
            }
        }
        if(y[q]>1){
            x[q]=i;
            q++;
        }
    }
    for(i=0;i<q;i++){
        for(j=0;j<q-i-1;j++)
            for(p=0;p<7;p++){
                    if(s[x[j+1]][p]>s[x[j]][p])break;
                    if(s[x[j+1]][p]<s[x[j]][p]){
                        t=x[j];
                        x[j]=x[j+1];
                        x[j+1]=t;
                        t=y[j];
                        y[j]=y[j+1];
                        y[j+1]=t;
                        break;
                    }
            }
    }
    return q;
}
int main()
{
    int n,i,j,k=0,x[50],y[50],q;
    char s[100][10];
    cin>>n;
    while(k<n){
        string str;
        int a;
        cin>>str;
        a=str.length();
        for(i=0;i<a;i++){
            if(str[i]=='-'){
                for(j=i;j<a-1;j++)str[j]=str[j+1];
            }
            if(str[i]=='A'||str[i]=='B'||str[i]=='C')str[i]='2';
            if(str[i]=='D'||str[i]=='E'||str[i]=='F')str[i]='3';
            if(str[i]=='G'||str[i]=='H'||str[i]=='I')str[i]='4';
            if(str[i]=='J'||str[i]=='K'||str[i]=='L')str[i]='5';
            if(str[i]=='M'||str[i]=='N'||str[i]=='O')str[i]='6';
            if(str[i]=='P'||str[i]=='R'||str[i]=='S')str[i]='7';
            if(str[i]=='T'||str[i]=='U'||str[i]=='V')str[i]='8';
            if(str[i]=='W'||str[i]=='X'||str[i]=='Y')str[i]='9';
            if(i==7){
                str[i]='\0';
                break;
            }
        }
        for(i=0;i<7;i++)s[k][i]=str[i];
        k++;
    }
    q=f(s,x,y,n);
  for(i=0;i<q;i++){
        for(j=0;j<3;j++)cout<<s[x[i]][j];
        cout<<'-';
        for(j=3;j<7;j++)cout<<s[x[i]][j];
        cout<<" "<<y[i]<<endl;
    }    
    //system("pause");
    return 0;
}

不认真读题,n最大值为100000,上面定义s[100]!!!

修改几小时后!!

#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
int temp[100002];
int comp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}
int change(string str)
{
    int a,b;
    a=str.length();
    for(int i=0;i<a;i++){
            if(str[i]=='-'){
                for(int j=i;j<a-1;j++)str[j]=str[j+1];
            }
            switch(str[i]){
            case 'A':
            case 'B':
            case 'C':str[i]='2';break;
            case 'D':
            case 'E':
            case 'F':str[i]='3';break;
            case 'G':
            case 'H':
            case 'I':str[i]='4';break;
            case 'J':
            case 'K':
            case 'L':str[i]='5';break;
            case 'M':
            case 'N':
            case 'O':str[i]='6';break;
            case 'P':
            case 'R':
            case 'S':str[i]='7';break;
            case 'T':
            case 'U':
            case 'V':str[i]='8';break;
            case 'W':
            case 'X':
            case 'Y':str[i]='9';break;
            }
            if(i==7){
                str[i]='\0';
                break;
            }
        }
        b=(str[0]-'0')*1000000+(str[1]-'0')*100000+(str[2]-'0')*10000+(str[3]-'0')*1000+(str[4]-'0')*100+(str[5]-'0')*10+(str[6]-'0');
        return b;
}
int main()
{
    int n,k=0,a=0,b=0;
    bool flag=false;
    cin>>n;
    while(k<n){
        string str;
        cin>>str;
        temp[k]=change(str);
        k++;
    }
    qsort(temp, n, sizeof(int),comp);
    for(int i=0;i<n;++i){
        if(i+1<n&&temp[a]==temp[i+1])b++;
        else{
            if(b){
                flag=true;
                printf("%03d-%04d %d\n", temp[a]/10000, temp[a]%10000, b+1);
                b=0;
            }
            a=i+1;
        }
    }
    if(!flag)    printf("No duplicates. \n");
   // system("pause");
    return 0;
}

然后是WA!!!!!快被自己蠢死了!
对比了一下正确代码与自己代码发现,我的代码是对输入字符串中存在7个数字或大写字母,超过7个,之后的数字或大写字母不计算到电话号码中。而正确的代码将所有数字或大写字母都计算在内,因此

在我的代码中 111111111和1111111111111111111是相同的电话号码

而正确代码中,两者是不同的!!!

这就是题目的缺陷,题目提到 输入的字符串中只有7个,但测试时必然有多于七个的字符串,才导致我的代码的结果错误。至少我研究了很久,是这样想的。

此题要注意到字符串与数字的转换,这样才可以运用快速排序,避免程序超时

然后正确代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>             
int telenum[100002];
int comp(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}
int main()
{

    int i, j, n, cnt, len, flag, low, sum;
    char temp[100];
    char alphalist[] = "2223334445556667777888999";
        scanf("%d", &n);
        for(i=0; i<n; ++i)
        {
            scanf("%s", temp);
            len = strlen(temp);
            sum = 0;
            for(j=0 ; j<len; ++j)
            {
                if('0' <= temp[j] && '9' >= temp[j])
                sum = sum*10 + (temp[j] - '0');
                else if('A'<= temp[j] && 'Z' >= temp[j])
                sum = sum*10 + (alphalist[temp[j]-'A']-'0');
            }
            telenum[i] = sum;
        }
        qsort(telenum, n, sizeof(int),comp);
        low = cnt = flag = 0;
        for(i=0; i<n; ++i)
        {
            if(i+1 < n && telenum[low] == telenum[i+1])
            cnt++;
            else 
            {
                if(cnt) 
                {
                    flag = 1;
                    printf("%03d-%04d %d\n", telenum[low]/10000, telenum[low]%10000, cnt+1);
                    cnt = 0;
                }
                low = i+1;
            }
        }
        if(!flag)    printf("No duplicates.\n");
    //system("pause");
    return 0;
}


 

 

posted @ 2016-01-30 18:02  Not-Bad  阅读(271)  评论(0编辑  收藏  举报