USACO Training Section 1.2 Name That Number 解题报告&AC代码

来源:互联网 发布:stc12c5a16s2 编程 编辑:程序博客网 时间:2024/05/19 00:13

解题报告:

这道题可以比较直白的办法,就是建立一个真假表格,验证是不是每一位都符合条件,一旦不符合,表格的这个位置为假,最后扫一遍表格,找出那些可以就行啦。

AC代码:

/*ID: yuanmz91PROG: namenumLANG: C++*/#include <fstream>#include <string.h>#include <memory.h>using namespace std;const int DICT_LEN = 5000;const int NAME_LEN = 15;bool compare(char, char);int main(){    ifstream fin("namenum.in");    ifstream dictin("dict.txt");    ofstream fout("namenum.out");    int mark = 0, len;    char dictionary[DICT_LEN][NAME_LEN];    char cowname[NAME_LEN];    bool finder[DICT_LEN], flag = false;    memset(finder, true, sizeof(finder));    while(dictin >> dictionary[mark++]);    fin >> cowname;    len = strlen(cowname);    for(int i = 0; i != mark; ++i)    {        if(strlen(dictionary[i]) != len)        {            finder[i] = false;        }    }    for(int i = 0; i != len; ++i)    {        for(int j = 0; j != mark; ++j)        {            if(!compare(cowname[i], dictionary[j][i]))            {                finder[j] = false;            }        }    }    for(int i = 0; i != mark; ++i)    {        if(finder[i])        {            fout << dictionary[i] << endl;            flag = true;        }    }    if(!flag)    {        fout << "NONE" << endl;    }    return 0;}bool compare(char x, char y){    switch(x)    {    case '2':        if((y == 'A') || (y == 'B') || (y == 'C'))        {            return true;        }        break;    case '3':        if((y == 'D') || (y == 'E') || (y == 'F'))        {            return true;        }        break;    case '4':        if((y == 'G') || (y == 'H') || (y == 'I'))        {            return true;        }        break;    case '5':        if((y == 'J') || (y == 'K') || (y == 'L'))        {            return true;        }        break;    case '6':        if((y == 'M') || (y == 'N') || (y == 'O'))        {            return true;        }        break;    case '7':        if((y == 'P') || (y == 'S') || (y == 'R'))        {            return true;        }        break;    case '8':        if((y == 'V') || (y == 'T') || (y == 'U'))        {            return true;        }        break;    case '9':        if((y == 'W') || (y == 'X') || (y == 'Y'))        {            return true;        }        break;    }    return false;}

下面是题目

英文版:

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
中文版:

下面是测试数据:

Here are the test data inputs:------- test 1 ----4734------- test 2 ----234643------- test 3 ----5747867437------- test 4 ----223------- test 5 ----532------- test 6 ----546------- test 7 ----53662------- test 8 ----5455426------- test 9 ----26678268463------- test 10 ----463373633623------- test 11 ----282742662------- test 12 ----463373633623------- test 13 ----2336------- test 14 ----5264------- test 15 ----426


原创粉丝点击