fengyunfly吧 关注:9贴子:102
  • 0回复贴,共1
const max_n=100;
    max_l=10;

var st:string;
tot,ans:longint;
art:array[0..max_n*max_l,1..26]of longint;
hou,fa:array[0..max_n*max_l] of longint;
bf:array[0..max_n*max_l]of longint;
val:array[0..max_n*max_l]of longint;
dan:array[0..max_n*max_l]of boolean;
procedure add;
var now,i,j:longint;
  begin
  now:=0;
  for i:=1 to length(st) do
    begin
    j:=ord(st[i])-96;
    if art[now,j]=0 then begin
      inc(tot);
      art[now,j]:= tot;
      fa[tot]:=now;
      val[tot]:=j;
      end;
    now:=art[now,j];
    end;
  dan[now]:=true;
  end;
procedure build;
var x,y,i,j:longint;
  begin
  x:=1; y:=2;
  fillchar(bf,sizeof(bf),0);
  bf[1]:=0;
  repeat
//  writeln(bf[x],'s turn');
  if fa[bf[x]]=0 then hou[bf[x]]:=0 else
    if val[bf[x]]<>0 then
      begin
      hou[bf[x]]:=art[hou[fa[bf[x]]],val[bf[x]]];
      dan[bf[x]]:=dan[bf[x]] or dan[hou[bf[x]]];
      end;
     // =0 de  ling wai kao lv
  if not dan[bf[x]] then
    begin
    for i:=1 to 26 do
      begin
      if art[bf[x],i]<>0 then begin
        bf[y]:=art[bf[x],i];
        inc(y);
        end else begin
        art[bf[x],i]:=art[hou[bf[x]],i];
        end;
      end;
    end;
  inc(x);
  until(x>=y);
  end;
procedure init;
var n,i,j:longint;
  begin
  fillchar(art,sizeof(art),0);
  fillchar(fa,sizeof(fa),0);
  fillchar(dan,sizeof(dan),false);
  fillchar(val,sizeof(val),0);
  tot:=0;
  readln(n);
  for i:=1 to n do
    begin
    readln(st);
    add;
    end;
  build;
{  for j:=0 to tot do
    begin
    for i:=1 to 26 do
     begin
     write(art[j,i]:2);
     end;
    writeln;
    end;             }
  end;
procedure main;
var now,i,j:longint;
  begin
  now:=0;
  readln(st);
  for i:=1 to length(st) do
    begin
    j:=ord(st[i])-96;
    now:=art[now,j];
    if dan[now] then begin writeln(i); exit; end;
    end;
  writeln('not dan');
  end;
begin
{assign();
assign();
reset();
rewrite();}
 init;
 main;
{close();
close();   }
end.


IP属地:上海1楼2008-06-23 23:37回复