poj 1002 stl map

来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/06/06 00:10
487-3279
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 198847 Accepted: 34639

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

124873279ITS-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




题意:

将输入的号码进行转化,使其变成***-****的形式,其中A~Z的字符可以按上给出的规则转化为数字(除Q与Z外),输出转换后号码的重复数大于1的号码以及其重复的次数。


思路:

其实就是一道简单模拟题。但是我是用STL中的map做的,虽然时间长了些,但是简单嘛,而且主要目的在练map。
不得不说,这道题首先做复杂了,还是因为map用的不熟。。。
只要一个map<string,int> a;
然后每次对一个号码转化后,就直接将其对应的int型的量加一即可。eg a[" "]++;
ps. 对于一开始map中每个string的对应的int值都为0.
即:cout<<a["string"];
输出值为0。
且map中key值(即此处的string)唯一

eg. a["string"]++
把“string”对应的int值改变成原来对应的加一。

针对题目,还有要注意的是:如果没有重复以上的号码,输出“No duplicates.”




代码:
View Code
 1 #include<iostream> 2 #include<stdio.h> 3 #include<map> 4 #include<string.h> 5 #include<string> 6 using namespace std; 7 //int times[100005]; 8 int main(){ 9     map <char,char> num;10     char i1='2';11     for(int i=1;i<=26;i1++){12             for(int j=1;j<=3;i++){13                     if(('Q'-'A'==i-1)||('Z'-'A'==i-1))14                          continue;15                     char temp=i+'A'-1;16                     num[temp]=i1;17                     j++;18                     }19                     }20    // cout<<num['C']<<" "<<num['F']<<" "<<num['W']<<" "<<num['Y']<<" "<<num['A']<<endl;21     int n;22     while(cin>>n){23      //  memset(times,0,sizeof(times));24        map <string,int> mapp;25        getchar();26        for(int i=1;i<=n;i++){27                char temp1[305];28                gets(temp1);29              //  cin>>temp1;30                char temp2[305];31                int lena=strlen(temp1);32                int lenb=0;33                for(int j=0;j<lena;j++){34                        if(temp1[j]!='-'){35                               if(temp1[j]<'Z'&&temp1[j]>='A'&&temp1[j]!='Q'&&temp1[j]!='Z'){36                                      temp2[lenb]=num[temp1[j]];37                                      }38                               else{39                                      temp2[lenb]=temp1[j];40                                      }41                               lenb++;42                                      }43                               }44                if(lenb!=7)45                     continue;46                string temp3=temp2;47                mapp[temp3]++;48                     }49        int sum=0;50        for(map<string,int>::iterator it=mapp.begin();it!=mapp.end();it++){51                                      if((it->second)>1){52                                          sum++;53                                          string temp5=it->first;54                                          for(int k=0;k<7;k++){55                                                  cout<<temp5[k];56                                                  if(k==2)57                                                      cout<<'-';58                                                      }59                                          cout<<" "<<it->second<<endl;60                                          }61                                      }62        if(sum==0)63           cout<<"No duplicates."<<endl;64                                      }65        return 0;66        }67                

 




原创粉丝点击