Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."
Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):
2: A,B,C 5: J,K,L 8: T,U,V
3: D,E,F 6: M,N,O 9: W,X,Y
4: G,H,I 7: P,R,S
Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the given dictionary which is supplied as dict.txt in the grading environment (and is sorted into ascending order).
For instance, the brand number 4734 produces all the following names:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI
GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI
GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI
HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI
HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI
IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI
ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
As it happens, the only one of these 81 names that is in the list of valid names is "GREG".
Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names. Serial numbers can be as many as a dozen digits long.
A single line with a number from 1 through 12 digits in length.
4734
A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.
GREG
题目大意:对给出的编号输出所有的有效名字(在dict.txt这个文件中),如果没有则输出"NONE''。
解题思路:简单的模拟。一个数字对应3个字母,如果我们先枚举出数字所代表的所有字符串,就有3^12种,一开始我的想法是根据给的数,列举出所有可能的字母组合,然后把这些可能的名字与字典里可接受的名字一一比较。后来发现这样做过于麻烦,会超时,所以换了一个算法。由于一个字母只对应一个数字,所以从字典中读入一个单词,把它转化成唯一对应的数字,看它是否与给出的数字匹配就可以了。同时设一个flag用来标记,初始值为0,找到一个符合要求的名字就把flag标记成1,等全部名字都处理完之后要是flag仍为0(即没有找到符合要求的名字),直接输出NONE就行了。另外,还可以把编号当字符串处理。
还可以根据字符串的长度进行剪枝,本题数据规模不大,不优化也可以。下面是我的代码:
/*
USER:xingwen wang
TASK:namenum
LANG:C
*/
#include
FILE *fp,*fin,*fout;
int map[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
int main()
{
int i,n,flag=0;
char str[13];
long long num,ans;
fp=fopen("dict.txt","r");
fin=fopen("namenum.in","r");
fout=fopen("namenum.out","w");
fscanf(fin,"%lld",&num); /*UASCO评测用的是linux,用%lld*/
while(fscanf(fp,"%s",str)!=EOF)
{
i=0;
ans=0;
while(str[i]!='\0')
{
ans*=10;
ans+=map[str[i]-'A'];
i++;
}
if(ans==num)
{
fprintf(fout,"%s\n",str);
flag=1;
}
}
if(!flag)
fprintf(fout,"NONE\n");
return 0;
}