USACO天梯--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
暴力即可解决的一道题,你可以把字典里面的字符串数字化,再进行比较。
以下是我的AC代码:
 
/*
ID:wang ming
PROG:namenum
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<fstream>
using namespace std;
int main()
{
	FILE *fin2=fopen("dict.txt","r"); 
	FILE *fin1=fopen("namenum.in","r");
	FILE *fout=fopen("namenum.out","w");
	char list[256];
list['A']=list['B']=list['C']='2';
list['D']=list['E']=list['F']='3';
list['G']=list['H']=list['I']='4';
list['J']=list['K']=list['L']='5';
list['M']=list['N']=list['O']='6';
list['P']=list['R']=list['S']='7';
list['T']=list['U']=list['V']='8';
list['W']=list['X']=list['Y']='9';
	char num[15];
	char word[15];
	char *p,*q;
	int cal=0;
	fscanf(fin1,"%s",&num);
	int lennum=strlen(num);
	while(fscanf(fin2,"%s",&word)!=EOF)
	{
		for(p=word,q=num;*p&&*q;p++,q++)
		{
			if(list[*p]!=*q)
			break;
		}
		if(*p=='\0'&&*q=='\0')
		{
			fprintf(fout,"%s\n",word);
			cal++;
		}
	}
	if(cal==0)
	fprintf(fout,"NONE\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值