Name That Number
Among the large Wisconsin cattle ranchers, it is customary to brand cowswith serial numbers to please the Accounting Department. The cow hands don'tappreciate the advantage of this filing system, though, and wish to call themembers 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 thebrand serial number of a cow into possible names uniquely associated with thatserial 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 toletters (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 acceptablecattle names (all letters capitalized). Take a cow's brand number and reportwhich of all the possible words to which that number maps are in the given dictionary whichis supplied as dict.txt in the grading environment (and is sorted intoascending order).
For instance, the brand number 4734 produces all the following names:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDHGRDI
GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEHGSEI
GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFHHPFI
HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDHHSDI
HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEHIPEI
IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFHIRFI
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 validnames is "GREG".
Write a program that is given the brand number of a cow and prints all thevalid names that can be generated from that brand number or ``NONE'' if thereare no valid names. Serial numbers can be as many as a dozen digits long.
PROGRAM NAME: namenum
INPUT FORMAT
A single line with a number from 1 through 12 digits inlength.
SAMPLE INPUT (file namenum.in)
4734
OUTPUT FORMAT
A list of valid names that can be generated from theinput, one per line, in ascending alphabetical order.
SAMPLE OUTPUT (file namenum.out)
GREG
老习惯,先题目大意:在农夫约翰养牛的地方,有一些傻逼当权者,他们要求每头牛按数字编号,然而农民们却不想叫自己的奶牛“4374号”之类的东西。。。他们当然还是叫自己的牛原来的名字,辣么要怎么把一个正常牛名翻译成一个标号呢?当地的统治者们想出这样一个好办法:把字母一一对应到手机按键上的字母(现在除了我还有几个人没用触屏机 = =!)然后把Q和Z挖掉。。。不知道他们在想什么。。。现在问题来了,给出一个标号和牛名字典(牛逼!),找出所符合标号的牛名。
于是楼主开始撸trie树了!于是楼主郁闷的内存超限了!直到楼主的一个朋友告诉楼主:只要把牛名字典里的每一个名字翻译成数字,再和题目给出的标号对比一下,一样的输出就行了。。。于是此题又被认为毫无难点。(除了要读入两个文件,一个是输入标号,一个是牛名字典)
代码如下:
/*
ID:su1q2d21
LANG:C++
TASK:namenum
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 20
#define lint long long
using namespace std;
char name[maxn];
int mp[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()
{
freopen("namenum.in","r",stdin);
freopen("namenum.out","w",stdout);
int n;
int t;
int i;
lint bn;
lint nat;
bool exist;
scanf("%lld",&bn);
freopen("dict.txt","r",stdin);
t=1;
exist=false;
while(scanf("%s",name)!=EOF){
n=strlen(name);
nat=0;
for(i=0;i<n;i++){
nat*=10;
if(name[i]>='A' && name[i]<='Z') nat+=mp[name[i]-'A'];
}
if(nat==bn){
printf("%s\n",name);
exist=true;
}
}
if(!exist) printf("NONE\n");
return 0;
}