487-3279
Time Limit:2000MS | Memory Limit:65536K | |
Total Submissions:209781 | Accepted:36555 |
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.
我在《ACM-ICPC程序设计系列 基础训练题解 余经善 朴秀峰 王宇华 于金峰 编著》中恰巧看到了此题的源程序,兴奋不已。。
源程序:
#include <iostream>
using namespace std;
typedef struct trietree * Ptree;
struct trietree //字典树
{
bool arrive; //单词结束位置
int treenum; //单词个数
Ptree next[10]; //字典树的儿子
}node[1000000];
int size; //字典树的规模
bool findsolve;
int dispose (char *p); //对字符串的处理,使之转化为数字串
void addnum(int num); //增加数字串
void newtree(int no); //建新树
void dfs(char phone[9],int m,Ptree p); /*深搜式地遍历整个字典树,由于是按数字的顺序遍历,输出的已经是排序后的串*/
int main()
{
int n; //串的个数
int i;
int number;
char phone[9]; //存储寻找使用的数字串
char ch[80];
scanf("%d",&n);
findsolve = false; //标记是否有重复的串,初始化为没有
size = 1;
newtree(1);
for( i=1;i<=n;i ++)
{
scanf("%s",ch);
number=dispose(ch); /*将字符串转化为一个整型数字,该整数的每一位存一个数字*/
addnum(number); //将转化后的数字加入字典树中
}
dfs(phone, 0, &node[1]); //遍历
if(!findsolve)
printf("No duplications.\n");
return 0;
}
void newtree(int no)
{
int i;
node[no].arrive = false;
node[no].treenum = 0;
for(i = 0;i<=9;i ++)
node[no].next[i]=NULL;
return ;
}
int dispose(char *p)
{
//该函数按照字母的顺序存入num中
int num = 0;
char *q = p;
while(*++p!='\0');
p--;
while(p>=q)
{
if(*p=='-')
{
p--;
continue;
}
num*=10;
if(*p>='A'&&*p<='Y')
num+=(*p-'A'-(*p>'Q'))/3+2;
else if(*p>='0'&&*p<='9')
num+=*p-'0';
p--;
}
return num;
}
void addnum(int num)
{
Ptree p=&node[1];
int i,k;
for(i = 0;i<=6;i++)
{
k=num%10;
num/=10;
if(!p->next[k]) //如果插入位置不存在,则创建该节点
{
newtree(++size);
p->next[k]=&node[size];
}
p=p->next[k];
}
p->arrive = true;
p->treenum ++;
return ;
}
void dfs(char phone[9], int m,Ptree p)
{
if(true == p->arrive) //如果该节点存在数
{
if(p->treenum > 1)
{
for(int i = 1;i<=7;i++)
{
if( i == 4) printf("-");
printf("%c",phone[i]);
}
printf(" %d\n",p->treenum);
findsolve = true;
}
return ;
}
for(int i=0;i<=9;i++)
{
if(p->next[i])
{
phone[m+1] = i+'0';
dfs(phone, m+1, p->next[i]);
}
}
return ;
}
运行截图: