比赛 noip_6 评测结果 AAATTTTTTT
题目名称 词链 最终得分 30
用户昵称 辨机ZN 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-10-26 11:36:15
显示代码纯文本
program ex(f1,f2);
 type
  zn=array [1..10000] of string;
  ka=array [1..50] of integer;
  sa=array [1..50,1..10000] of string;
 var
  a:zn; i,j,k,m,n,l,p,ans,maxl:longint;
  f1,f2:text; s:string;  b:sa; head,len:ka;
 procedure find(ss:string; now:integer; tou:integer);
  var
   i,j,p,l:integer;
  begin
   if now+maxl-length(ss)<ans then exit
   else begin
   for i:=tou to n do
     if pos(ss,a[i])=1 then
      begin
       l:=length(a[i]);
       for j:=l+1 to 50 do if head[j]<>0 then break;
       find(a[i],now+1,head[j]);
      end;
   if now>ans then ans:=now;
         end;
  end;



 begin
  assign(f1,'link.in'); reset(f1);
  assign(f2,'link.out'); rewrite(f2);
  readln(f1,n);
  for i:=1 to 50 do len[i]:=0;
  for i:=1 to n do
   begin
    readln(f1,s);
    l:=length(s);
    inc(len[l]);
    b[l,len[l]]:=s;
   end;
  p:=0;

  for i:=1 to 50 do
   for j:=1 to len[i] do
    begin
     inc(p);
     a[p]:=b[i,j];
    end;
  for i:=50 downto 1 do
   if len[i]<>0 then begin maxl:=i; break; end;
  head[1]:=1;
  for i:=2 to 50 do head[i]:=head[i-1]+len[i-1];
  ans:=0;
  for i:=2 to 50 do if head[i]<>0 then break;
  find(a[1],1,head[i]);
  writeln(f2,ans);
  close(f1);
  close(f2);
 end.