USACO 1.2.3 Name That Number 命名那个数字

Name That Number

Among the large Wisconsin cattle ranchers, it is customary to brandcows with serial numbers to please the Accounting Department. The cowhands don't appreciate the advantage of this filing system, though, andwish to call the members of their herd by a pleasing name rather thansaying, "C'mon, #4734, get along."

Help the poor cowhands out by writing a program that will translatethe brand serial number of a cow into possible names uniquely associatedwith that serial number. Since the cow hands all have cellular saddlephones these days, use the standard Touch-Tone(R) telephone keypadmapping 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,000acceptable cattle names (all letters capitalized). Take a cow'sbrand number and report which of all the possible words to whichthat number maps are in the givendictionary which is supplied as dict.txt in the gradingenvironment (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 listof valid names is "GREG".

Write a program that is given the brand number of a cow andprints all the valid names that can be generated from that brandnumber or ``NONE'' if there are no valid names. Serial numbers canbe 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 perline, in ascending alphabetical order.

SAMPLE OUTPUT (file namenum.out)

GREG


解题思路:
        该题要求有两个,其一、将输入的数字转换成对应该的字母组合,然而这个过程是非常麻烦的;其二、根据翻译出来的一大堆字母组合,在某文件中查找,若存在,
则输出,若翻译的所有组合在文件中均不存在,则输出“NONE”。那么,反过来想,我们对已知的文件进行扫描,并进行反向翻译,将字母组合翻译成数字组合,并与输入数字
比较若相等,则输出该字母组合,并继续扫描,若最终一个都没有,则输出“NONE”,时间复杂度O(n)。
代码如下:
/*
ID:ayludon3
LANG: C++
TASK: namenum
*/
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;

char transf(char ch)
{
    switch(ch)
    {
        case 'A':return '2'; case 'B':return '2'; case 'C':return '2';
        case 'D':return '3'; case 'E':return '3'; case 'F':return '3';
        case 'G':return '4'; case 'H':return '4'; case 'I':return '4';
        case 'J':return '5'; case 'K':return '5'; case 'L':return '5';
        case 'M':return '6'; case 'N':return '6'; case 'O':return '6';
        case 'P':return '7'; case 'R':return '7'; case 'S':return '7';
        case 'T':return '8'; case 'U':return '8'; case 'V':return '8';
        case 'W':return '9'; case 'X':return '9'; case 'Y':return '9';
    }
    return 0;
}
bool cmp(char a[],char b[],int len1,int len2 )
{
     int i;
     if(len1!=len2)
        return false;
     for(i=0;i<len1;i++)
          if(a[i]!=b[i])
             return false;
     return true;
}

int main()
{
    ifstream fin("namenum.in");
    ofstream fout("namenum.out");
    ifstream data;
    char str[13],cur[13];
    int len1,len2;
    char temp[13];
    int i,flag=0;
    data.open("dict.txt");
    fin>>str;
    len2=strlen(str);
    while(!data.eof())
    {
        data>>cur;
        len1=strlen(cur);
        for(i=0;i<len1;i++)
            temp[i]=transf(cur[i]);
        if(cmp(temp,str,len1,len2))
        {
            flag=1;
            fout<<cur<<endl;
        }
    }
    if(!flag)
        fout<<"NONE"<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值