标签:
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.
4734
GREG
老规矩直接上翻译地址 http://www.nocow.cn/index.php/Translate:USACO/namenum
通过相应数字转化为字母 并在dict.txt中查找相应单词匹配
我是直接在dict中搜索 每个单词转化为相应的数字 并进行匹配 简单的字符串处理 因为数据范围不大 所以感觉效率还好。。。
dict文件的处理不会 捂脸 现场百度学习的。。orz
/* ID: Augur Alpha LANG: C++ TASK: namenum */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const char code[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'}; char str[13], temp[13], cnt[13]; int main(){ freopen("namenum.in", "r", stdin); freopen("namenum.out", "w", stdout); freopen("dict.txt", "r", stderr); bool flag = false; gets(str); while(fscanf(stderr, "%s", temp)!=EOF){ if(code[temp[0]-'A'] != str[0]){ continue; } int len = strlen(temp); for(int i = 0; i < len; ++i){ cnt[i] = code[temp[i]-'A']; } cnt[len] = '\0'; if(!strcmp(str, cnt)){ flag = true; puts(temp); } } if(!flag){ puts("NONE"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
【USACO1.2.3】Name That Number(字符串处理)
标签:
原文地址:http://blog.csdn.net/u012431590/article/details/46830781