usaco 29 name that number

来源:互联网 发布:最新编程语言 编辑:程序博客网 时间:2024/05/16 07:43
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 dictionarywhich 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




这是一道比较有趣的题目;

主要是一个转化的思想。。

想一下,要判断dict文件中与输入的字符串吻合的串,不必将输入的串按字符一个个转化,只需要将文件中的串按字符转化成数字,然后与输入的串相比较即可,相等的
就输出,不等的则过。



代码:

View Code
 1 /* 2 ID: jings_h1 3 PROG: namenum 4 LANG: C++ 5 */ 6  7 #include<iostream> 8 #include<fstream> 9 #include<string.h>10 using namespace std;11 int main(){12     char a[30];13     a[0]=a[1]=a[2]='2';14     a[3]=a[4]=a[5]='3';15     a[6]=a[7]=a[8]='4';16     a[9]=a[10]=a[11]='5';17     a[12]=a[13]=a[14]='6';18     a[15]=a[17]=a[18]='7';19     a[19]=a[20]=a[21]='8';20     a[22]=a[23]=a[24]='9';21     char a1[15];22     ofstream fout ("namenum.out");23     ifstream fin ("namenum.in");24     ifstream fin1("dict.txt");25     fin>>a1;26            int len=strlen(a1);27            char word[15];28            int times=0;29            while(fin1>>word){30            int i;31            for(i=0;i<len;i++){32                    if(a[word[i]-'A']!=a1[i]){33                         break;34                         }35                         }36            if(i>=len){37                 if(strlen(word)==len){38                   fout<<word<<endl;39                   times++;40                 }41                 }42                 }43     if(times==0)44       fout<<"NONE"<<endl;45     return 0;46 }

 

原创粉丝点击