题目:就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10)。
分析:直接就是裸的字典树。听说还可以快排+二分或者hash之类的,但我懒得写了。
代码:
var str:string; i,a1,x,len:longint; a:array[1..1000000] of longint; w:array[1..1000000] of string; s:array[1..1000000] of char; son:array[1..1000000,1..26] of longint; procedure insert(x:longint;str,str1:string); var i:longint; begin if str='' then begin w[x]:=str1; exit; end; for i:=1 to a[x] do if s[son[x,i]]=str[1] then begin delete(str,1,1); insert(son[x,i],str,str1); exit; end; inc(a1); inc(a[x]); son[x,a[x]]:=a1; s[a1]:=str[1]; delete(str,1,1); insert(a1,str,str1); end; function find(x:longint;str:string):string; var i:longint; begin find:='eh'; if str='' then begin if w[x]<>'' then find:=w[x]; exit; end; for i:=1 to a[x] do if s[son[x,i]]=str[1] then begin delete(str,1,1); find:=find(son[x,i],str); exit; end; end; begin readln(str); a1:=1; while length(str)>=1 do begin x:=pos(' ',str); len:=length(str); insert(1,copy(str,x+1,len-x),copy(str,1,x-1)); readln(str); end; while not eof do begin readln(str); writeln(find(1,str)); end; end.