Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Trie水过~~Memory 63552KB~~

Posted by noipchampion at 2011-01-25 14:21:49 on Problem 2503 and last updated at 2011-01-25 14:22:10
type
  node=record
         ch:array['a'..'z'] of longint;
         link:longint;
  end;

var trie:array[1..1000000] of node;
    en,fo:array[1..100000] of string;
    n,p,l:longint;
    st:string;

procedure insert(s:string);
  var r,i:longint;

  begin
    r:=1;
    for i:=1 to length(s) do
      if trie[r].ch[s[i]]=0 then
        begin
          l:=l+1;
          trie[r].ch[s[i]]:=l;
          r:=l;
        end
      else
        r:=trie[r].ch[s[i]];
    trie[r].link:=n;
  end;

function find(s:string):string;
  var r,i:longint;

  begin
    r:=1;
    for i:=1 to length(s) do
      if trie[r].ch[s[i]]<>0 then
        r:=trie[r].ch[s[i]]
      else
       exit('eh');
    exit(en[trie[r].link]);
  end;

begin
  n:=0;
  l:=1;
  readln(st);
  while length(st)>=1 do
    begin
      n:=n+1;
      p:=pos(' ',st);
      en[n]:=copy(st,1,p-1);
      fo[n]:=copy(st,p+1,length(st)-p);
      insert(fo[n]);
      readln(st);
    end;
  while not(eof) do
    begin
      readln(st);
      writeln(find(st));
    end;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator