• 【USACO】


    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

    简单题,主要是不能从输入的数字找字母串,应该从dict里面的字母串推出数字,然后和给定的数字比较是否相等。
    这里其实把数字也当作字符串处理了,因为用string非常方便。用了一个空间换时间的方法,就是设定数组word_to_number,它的每个元素对应相应的字母(A~Z)对应的数字。所以将字符串转换为数字的时候就可以直接用这个数组一步做到了。
    代码如下:
     1 /*ID:Moment1991
     2   PROG:namenum
     3   LANG:C++
     4   Compiling...
     5   Compile: OK
     6 
     7   Executing...
     8    Test 1: TEST OK [0.019 secs, 3496 KB]
     9    Test 2: TEST OK [0.019 secs, 3496 KB]
    10    Test 3: TEST OK [0.016 secs, 3496 KB]
    11    Test 4: TEST OK [0.014 secs, 3496 KB]
    12    Test 5: TEST OK [0.019 secs, 3496 KB]
    13    Test 6: TEST OK [0.016 secs, 3496 KB]
    14    Test 7: TEST OK [0.014 secs, 3496 KB]
    15    Test 8: TEST OK [0.019 secs, 3496 KB]
    16    Test 9: TEST OK [0.014 secs, 3496 KB]
    17    Test 10: TEST OK [0.022 secs, 3496 KB]
    18    Test 11: TEST OK [0.019 secs, 3496 KB]
    19    Test 12: TEST OK [0.022 secs, 3496 KB]
    20    Test 13: TEST OK [0.024 secs, 3496 KB]
    21    Test 14: TEST OK [0.019 secs, 3496 KB]
    22    Test 15: TEST OK [0.024 secs, 3496 KB]
    23 
    24 All tests OK
    25  */
    26 #include <iostream>
    27 #include <fstream>
    28 using namespace std;
    29 int main(){
    30     ifstream inDict("dict.txt");
    31     ifstream cin("namenum.in");
    32     ofstream cout("namenum.out");
    33     string number;
    34     bool find = false;
    35 
    36     cin >> number;
    37     string word_to_number[26] = {"2","2","2","3","3","3","4","4","4","5","5","5","6","6","6","7","7","7","7","8","8","8","9","9","9","9"};
    38 
    39     string word;
    40     while(inDict >> word){
    41         string num = "";
    42         //word转换成对应的数字
    43         for(int i = 0;i < word.size();i++){
    44             num += word_to_number[word[i]-'A'];
    45         }
    46 
    47         //word转换得到的数字和输入的数字比较,如果相等就输出word
    48         if(num == number)
    49         {
    50             find = true;
    51             cout << word<<endl;
    52         }
    53     }
    54 
    55     if(!find)
    56         cout << "NONE"<<endl;
    57 }
  • 相关阅读:
    [JLOI2011] 飞行路线
    高精度运算模板
    Dijkstra算法模板
    [SDOI2010] 外星千足虫
    [SDOI2006] 线性方程组
    [CTSC2014] 企鹅QQ
    模板三连击:树状数组+线段树+主席树
    [ZJOI2008] 树的统计
    [国家集训队] 礼物
    [洛谷P4720] [模板] 扩展卢卡斯
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3745279.html
Copyright © 2020-2023  润新知