USACO 1.3 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.

在威斯康辛州牛大农场经营者之中,都习惯于请会计部门用连续数字给母牛打上烙印. 但是,母牛用手机时并没感到这个系统的便利,它们更喜欢用它们喜欢的名字来呼叫它们的同伴,而不是用像这个的语句"C'mon, #4734, get along.". 请写一个程序来帮助可怜的牧牛工将一只母牛的烙印编号翻译成一个可能的名字. 

因为母牛们现在都有手机了,使用那标准的按键的排布来把收到从数目翻译到文字:( 除了为之外 "Q" 和 "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 

可接受的名字都被放在这样一个叫作 dict.txt 的文件中,它包含一连串的少于 5000 个可接受的牛 名字.(所有的名字都是大写的),收到母牛的编号返回那些能从编号翻译出来并且在字典中的名字. 举例来说,编号 4734 能产生的下列各项名字: 

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 

碰巧,81 个中只有一个"GREG"是有效的(在字典中). 

写一个程序来对给出的编号打印出所有的有效名字,如果没有则输出"NONE'' . 编号可能有 12 位数字.

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


题解:
一个数字对应 3 个字母,如果我们先枚举出数字所代表的所有字符串,就有 3^12 种,
然后再在 5000 的字典里寻找,可以用二分查找,数据规模是 3^12*log5000=6.5e6,空间规模是 5000。 
其实可以做的更好! 
一个字母只对应一个数字,从字典中读入一个单词,把它转化成唯一对应的数字,看它是否与给出的数字匹配,时间规模是 5000*12=6e4,空间规模是常数,而且编程复杂度较低。 
还可以先比较字符长度和数字长度,如果相等,逐位比较。 
把字母对应成数字我给出个公式,证明不写了,很容易想明白:
temp:=ord(nam[i][j])-64;if temp>17thendec(temp); 
num[i]:=num[i]*10+((temp-1) div3)+2;
temp 是第 i 个名字的第 j 个字符 ASCII 码-64 后的值,num[i]是地 i 个名字转换成的数字。
还有一个方法,利用哈希表,把字典进行哈希. 然后对于新产生的字符串,在哈希表里进行查找,找到了则加入输出列表. 最后则快排后输出。 
补充一下:可以用集合来处理 s[2]=['A','B','C'];s[3]=['D','E','F']; 由于以3为周期,这种集合的建立可以由 div3 得到但注意其中挖掉了'Q',这可以分两段来建立集合。 
另一种思路:如果我们就使用普通的搜索,可以这样优化:设一个有效奶牛名数量统计s,在读字典时,首先粗略判断字典中的奶牛名是否是我们所要的(如:长度是否和input文件里一样,首字母代表的数字是否与input文件中的数字一样等等。注意input文件中的数字最好读成字符串处理。)如果不合法, s就不加 1. 这样最终剪枝之后,每个数据的有效名称不超过 200 个,算最坏情况,时间复杂度共计 O(5000+200)计 ,可以说是非常优秀。


代码如下:
/*
ID: zhaoxia13
LANG: C
TASK: namenum
*/

const char code[26] = {'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};
char a[13], b[13];
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
	freopen("namenum.in", "r", stdin);
	freopen("namenum.out", "w", stdout);
	freopen("dict.txt", "r", stderr);
	int flag = 1, flg = 1, i;
	scanf("%s", a);
	while(fscanf(stderr, "%s", b) != EOF)
	{
		flg = 1;
		if(strlen(a) != strlen(b))
			continue;
		for(i = 0; b[i]; i++)
			flg = flg && (code[b[i]-'A'] == a[i]);
		if(flg)
		{flag = 0; printf("%s\n", b);}
	}
	if(flag)
		printf("NONE\n");

	fclose(stdin);
	fclose(stdout);
	fclose(stderr);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值