USACO1.2.3Name That Number

Name That Number

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.

PROGRAM NAME: namenum

INPUT FORMAT

A single line with a number from 1 through 12 digits in length.

SAMPLE INPUT (file namenum.in)

4734

OUTPUT FORMAT

A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.

SAMPLE OUTPUT (file namenum.out)

GREG
题解:每个数字对应3个字母,如果先枚举出数字代表的字符串,那么最大数据有3^12=531441种。虽然能AC,但是还是效率太低了,所以可以考虑另外一种方法,一个数字对应3个字母,但是一个字母只对应一个数字!。所以可以把字母转换成数字,然后比较。枚举量是5000。
View Code
 1 /*
 2 ID:spcjv51
 3 PROG:namenum
 4 LANG:C
 5 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 #include<ctype.h>
 9 int main(void)
10 {
11     freopen("namenum.in","r",stdin);
12     freopen("namenum.out","w",stdout);
13     int num[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};
14     char name[13];
15     int len1,len2,flag;
16     char listt[13];
17     flag=0;
18     scanf("%s",name);
19     len1=strlen(name);
20     freopen("dict.txt","r",stdin);
21     while(scanf("%s",listt)!=EOF)
22     {
23              int i;
24              i=0;
25              len2=strlen(listt);
26              if(len1==len2)
27              {
28                   while((name[i]-'0')==(num[listt[i]-'A'])&&i<len1)i++;
29                   if(i==len1)
30                   {
31                       printf("%s\n",listt);
32                       flag=1;
33                   }
34 
35              }
36    }
37     if(flag==0) printf("NONE\n");
38    return 0;
39 }


原文地址:https://www.cnblogs.com/zjbztianya/p/2853159.html