zoukankan      html  css  js  c++  java
  • USACO Name That Number

    题目:

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

    解答过程:

    第一次:每次获得一个组合然后,在文件中查找。

                多次的IO,浪费时间

    第二次:顺序依次读取文件中的name到一个数组中,每次在数组中查找

               使用2分查找,否则又超时

    给出的答案:我没有想到的是反方向的查看,每次读取文件一个name后就映射到一个数字串,比较是否是符合要求的,这样只需要顺序遍历文件依次就可以获得所有结果,而且不需要存储文件中的信息到内存中。

  • 相关阅读:
    实现基于C++的动态事件机制(转)
    D3D HLSL 一段代码注释
    directx加载ms3d动画模型
    几种空间分割算法研究之bsp
    IxEngine开发笔记
    使用UVAtlas技术管理贴图
    八叉树
    c#调用C++写的dll导出类,包含继承,重载等详细介绍(转载)
    给自己毕业前的一点任务
    大小端问题
  • 原文地址:https://www.cnblogs.com/growup/p/1971526.html
Copyright © 2011-2022 走看看