Trie
program trie; Var ch:array[0..1000,'A'..'z'] of longint; isword:array[0..1000] of boolean; top:longint; Procedure insert(p:string); var k,now:longint; begin k:=1;now:=1; while k<=length(p) do begin if not (ch[now,p[k]]<>0) then begin inc(top); ch[now,p[k]]:=top; end; now:=ch[now,p[k]]; inc(k); end; isword[now]:=true; end; Function search(P:string):longint; var k,now:longint; begin k:=1; now:=1; while k<=length(p) do begin if ch[now,p[k]]=0 then exit(0); now:=ch[now,p[k]]; inc(k); end; if isword[now] then exit(now) else exit(0); end; begin top:=1; insert('a'); insert('abandon'); insert('abandoned'); insert('abashed'); writeln(search('abashed')); writeln(search('abash')); readln end.