Stop the Hollyweb! No DRM in HTML5.

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.

 

posted on 2013-05-17 19:58  灰天飞雁  阅读(237)  评论(0编辑  收藏  举报

填写您的邮件地址,订阅我们的精彩内容:  点击这里给我发消息

添加到收藏夹