题目: | 命名那个数字 | |
来源: | Usaco1.2.3 | |
题目大意: | 给定一个数字,求他所组成的符合规定的字符串 | |
数据范围: | 数字长度1~12 | |
样例: |
4734 |
GREG |
做题思路: | 本来写的深搜加二分查找结果无力的挂了,换一种思路更健康。看来还是筛表实惠,把 表中符合该数字的输出,就是把题意反过来想了。 | |
知识点: | 合理分析题意、搜索、文件读入 |
{
ID:Dount Nameless
TASK:namenum
LANG:PASCAL
}
var
j,len:longint;
s,num,r:string;
f,t,k:boolean;
c:char;
begin
assign(input,'namenum.in');reset(input);{<先读数字>}
readln(num);
close(input);
assign(input,'dict.txt');reset(input);{<再读文件>}
assign(output,'namenum.out');rewrite(output);
f:=false;{<标记是否有答案>}
while not eof do
begin
readln(s);{<读入表中的每一个字符串>}
if(ord(s[1])-ord('A'))div 3+2=ord(num[1])-ord('0') then k:=true elsek:=false;{<先处理下第一个字母与第一个数字,进行减值>}
ifk then t:=true;
if(length(num)=length(s))and k then{<长度相等的进入下一步比较>}
begin
t:=true;
len:=length(s);
for j:=1 to len do
begin
if (s[j]='S') or (s[j]='V') or (s[j]='Y') thenc:=chr((ord(s[j])-ord('A'))div 3+1+ord('0'))
else c:=chr((ord(s[j])-ord('A'))div 3+2+ord('0'));{<将字母转化成数字,注意规律>}
if c<>num[j] then break;
if j=len then {<判断通过输出>}
begin
f:=true;
writeln(s);
end;
end;
end;
end;
ifnot f then writeln('NONE');{<无答案则输出none>}
close(input);close(output);
end.
题目来源: http://ace.delos.com/usacoprob2?a=RexmW8CclSt&S=namenum