USACO Name That Number 解题日志

来源:互联网 发布:网络信号屏蔽器原理 编辑:程序博客网 时间:2024/05/29 18:10

先贴上题目吧。

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 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

把输入的数字处理为一个字符串,定义为string类型,便于直接用 “==”来比较。

开始是想把数字转化成字母,再去给的字典里去找。然而当时没想起如何去找。

后来突然想起,用一个while(num != newname)的循环也许可以,但是这里有个问题,比如他的第三组数据,可以得到两个合法的名字,而这样只有一个。并且,循环里面要怎么写,有个细节我也不知道。。。

所以就用了把单词转化成string类型的数字的方法,再与所给字典一个一个比较下去,输出所有合法的名字即可。


代码如下:

#include<iostream>#include<fstream>#include<string>using namespace std;string name;ifstream fin("namenum.in");ifstream fin_dict("dict.txt");ofstream fout("namenum.out");string name2num(string name){for(int i = 0; i < name.length(); ++i){if (name[i] >= 'A' && name[i] <= 'C') {name[i] = '2'; continue;}if (name[i] >= 'D' && name[i] <= 'F') {name[i] = '3'; continue;}if (name[i] >= 'G' && name[i] <= 'I') {name[i] = '4'; continue;}if (name[i] >= 'J' && name[i] <= 'L') {name[i] = '5'; continue;}if (name[i] >= 'M' && name[i] <= 'O') {name[i] = '6'; continue;}if (name[i] == 'P' || name[i] == 'R' || name[i] == 'S') {name[i] = '7'; continue;}if (name[i] >= 'T' && name[i] <= 'V') {name[i] = '8'; continue;}if (name[i] >= 'W' && name[i] <= 'Y') {name[i] = '9'; continue;}}return name;}int main(){string num;fin >> num;bool flag = 0;while (fin_dict >> name){string mid = name;string newname = name2num(name);//fout << newname << endl;if (num == newname){fout << mid << endl;flag = 1;}}if (!flag) fout << "NONE" << endl;fin_dict.close();fin.close();fout.close();return 0;}

0 0
原创粉丝点击