现在的位置: 首页 > 综合 > 正文

【多文件操作】【检索】Name That Number (Usaco_Training 1.2)

2014年02月09日 ⁄ 综合 ⁄ 共 3114字 ⁄ 字号 评论关闭
文章目录

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

 

 

首先是多文件,可以用FILE* in=("test.in","r");FILE* out=("test.out","w"); 来完成,最后关闭 fclose(in);fclose(out);
头文件#inlcude<cstdio>        输入输出用fscanf和fprint

还有一种就是标准C++的文件流,要包含头文件#include<fstream>
ifstream fin("test.in");ofstream fout("test.out");  最后关闭fin.close();fout.close();
中间输入输出和cin cout用法一样,用fin和fout

接下来就是检索,有两种方法,一种吧输入数据转化成字母,然后组合排列,然后由于字典中是按字母顺序排的,可以二分查找,规模较小,应该能过。。。。

还有一种方法就是把字典读入后,处理成数字,然后跟输入数据比对就ok了,总体时间效率是O(3*4617),还是比较快

C++ Code

/*
ID: jiangzh15
TASK: namenum
LANG: C++

http://blog.csdn.net/jiangzh7

*/
#include<cstdio>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

struct node{string s,num;}a[5000];
int key['Z'+10];

void makemap()
{
    key['A']=key['B']=key['C']=2;
    key['D']=key['E']=key['F']=3;
    key['G']=key['H']=key['I']=4;
    key['J']=key['K']=key['L']=5;
    key['M']=key['N']=key['O']=6;
    key['P']=key['Q']=key['R']=key['S']=7;
    key['T']=key['U']=key['V']=8;
    key['W']=key['X']=key['Y']=key['Z']=9;
}

void readdict()
{
    ifstream fin("dict.txt");
    for(int i=1;i<=4617;i++) fin>>a[i].s;
    fin.close();
    for(int i=1;i<=4617;i++)
        for(int j=0;j<a[i].s.length();j++)
            a[i].num+=key[a[i].s[j]]+'0';
}

int main()
{
    makemap();
    readdict();
    freopen("namenum.in","r",stdin);
    freopen("namenum.out","w",stdout);
    string tmp;
    cin>>tmp;
    bool flag=false;
    for(int i=1;i<=4617;i++)
        if(a[i].num==tmp)
            {cout<<a[i].s<<endl;flag=true;}
    if(!flag)cout<<"NONE"<<endl;
    return 0;
}

 

 

抱歉!评论已关闭.