USACO section 1.2 Name That Number

来源:互联网 发布:淘宝商品老是被删除 编辑:程序博客网 时间:2024/05/16 10:22
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 thegiven 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 GRDIGREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEIGSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFIHRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDIHSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEIIPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFIISDG 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

译文翻译(出自sznoi)

在威斯康辛州牛大农场经营者之中,都习惯于请会计部门用连续数字给母牛打上烙印。
但是,母牛用手机时并没感到这个系统的便利,它们更喜欢用它们喜欢的名字来呼叫它们的同伴,而不是用像这个的语句"C'mon, #4734, get along."。
请写一个程序来帮助可怜的牧牛工将一只母牛的烙印编号翻译成一个可能的名字。
因为母牛们现在都有手机了,使用那标准的按键的排布来把收到从数目翻译到文字:( 除了为之外 "Q" 和 "Z")

2: A,B,C 5: J,K,L 8: T,U,V3: D,E,F 6: M,N,O 9: W,X,Y4: G,H,I 7: P,R,S可接受的名字都被放在这样一个叫作"dict.txt" 的文件中,它包含一连串的少于 5,000个可接受的牛名字。 (所有的名字都是大写的)收到母牛的编号返回那些能从编号翻译出来并且在字典中的名字。举例来说,编号 4734 能产生的下列各项名字:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDIGREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEIGSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFIHRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDIHSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEIIPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFIISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI碰巧,81个中只有一个"GREG"是有效的(在字典中)。Challenge One写一个程序来对给出的编号打印出所有的有效名字,如果没有则输出"NONE'' 。编号可能有12位数字。

PROGRAM NAME: namenum

INPUT FORMAT
单独的一行包含一个编号(长度可能从1到12)。

SAMPLE INPUT (file namenum.in) 
4734

OUTPUT FORMAT
以字典顺序输出一个有效名字的不负列表,一行一个名字。

SAMPLE OUTPUT (file namenum.out)
GREG


开时做这道题的时候我在程序的前面写了这样几行注释:
//what to do?
//first,read in the Dictainary
//second,read in the number
//third,find the all possible case of the name
//forth,search the Dictainary to find the name
//fifth,output the name or NONE
这些注释表现了我最开时的想法(事实证明这也是最笨的方法),然后我就尝试来按上面的一步一步来做,到了最后我发现程序运行到输出结果用的时间大大的超出了限制,于是我就去各位牛人的博客寻找解决方案,我看到各位牛人的想法跟我完全不同,他们不都是把所有可能的名字都列一边,而是直接对字典进行处理,把字典里的名字换成数字再去和给的数字进行比较,看到了这些我恍然大悟,然后又重新写代码交了上去.

这里有几点需要注意(也是我犯过的错误):
读入数字的时候要用一个字符数组来存,因为题目中说了数字的长度是从1到12不等的,如果是12位早就超出int的范围了,这一点要特别注意.
数字的存放顺序要一致,读入的数是从高位到低位,那么处理字典的时候也要从高位到低位.
输出末尾要有新行

C代码:

#include <stdio.h>  #include <string.h>  #include <math.h>    char Dict[5000][20];//如果想要省空间的话这个数组完全可以不要  char numsplit[13];  int num[13];  int a[100];  int DictSize,len,numlen;    int isEqual(int *a, int lena, char *b, int lenb);    int main()  {      int i=0,j,number=0,n=0;      int flag=1;      freopen("namenum.out","w",stdout);  //对字符初始化 将字母和手机上的键盘对应      a[65] = 2; a[66] = 2; a[67] = 2;  a[68] = 3; a[69] = 3; a[70] = 3;  a[71] = 4; a[72] = 4; a[73] = 4;  a[74] = 5; a[75] = 5; a[76] = 5;  a[77] = 6; a[78] = 6; a[79] = 6;  a[80] = 7; a[82] = 7; a[83] = 7;  a[84] = 8; a[85] = 8; a[86] = 8;  a[87] = 9; a[88] = 9; a[89] = 9;  freopen("namenum.in","r",stdin);    scanf("%s",numsplit);    numlen=strlen(numsplit);    for(i=0;i<numlen;i++) {      numsplit[i]-='0';//将字符读入的数字都处理一下,后面好比较  }    i=0;    freopen("dict.txt","r",stdin);  //这里把读入和处理放到一起了 这样就减少了对字典的操作次数  while(scanf("%s",&Dict[i])!=EOF) {//直到文件末尾      getchar();      len = strlen(Dict[i]);      n=0;      for(j=0;j<len;j++) {          num[j]=a[Dict[i][j]];//把字典里的名字翻译成数字      }      if(isEqual(num,len,numsplit,numlen)) {//比较          flag=0;          printf("%s\n",Dict[i]);      }      i++;  }  if(flag)printf("NONE\n");  return 0;  }//比较两个数的大小  //是专门为这个写的所以第二个数组是字符数组  int isEqual(int *a, int lena, char *b, int lenb) {      int i;      if(lena!=lenb)return 0;      for(i=0;i<lena;i++) {          if(a[i]!=b[i])return 0;      }      return 1;  }  





原创粉丝点击