codevs1018

来源:互联网 发布:linux shell有哪几种 编辑:程序博客网 时间:2024/06/07 03:05

题目地址:http://codevs.cn/problem/1018/

分析:

深搜

代码:

var word:array[1..200] of string;

      w:array[1..200] of longint;

      i,n:longint;

      a:string;

      dragon:string;

      maxlen:longint;

procedure search(s:string;x:longint);

var i:longint;

      len:string;

      max:longint;

begin

      max:=0;

      len:=copy(s,length(s)-x+1,255);

      if len <> s then

      begin

            for i := 1 to n do if w[i] <> 2 then

            begin

                  if pos(len,word[i]) > max then max:=pos(len,word[i]);

                  if pos(len,word[i]) = 1 then 

                  begin

                        s:=s+copy(word[i],length(len)+1,255);

                        inc(w[i]);

                        search(s,1);

                        dec(w[i]);

                        delete(s,length(s)-length(copy(word[i],length(len)+1,255))+1,255);

                  end;

            end;

            if max = 0 then 

            begin

                  if length(s) > maxlen then maxlen:=length(s);

                  exit;

            end;

            search(s,x+1);

      end

      else

      if length(s) > maxlen then maxlen:=length(s);

      end;


begin

      fillchar(w,sizeof(w),0);

      readln(n);

      for i := 1 to n do readln(word[i]);

      read(a);

      for i := 1 to n do 

      begin

            if pos(a,word[i]) = 1 then 

            begin

                  dragon:=word[i];

                  inc(w[i]);

                  search(dragon,1);

                  dec(w[i]);

            end;

      end;

      write(maxlen);

end.


0 0
原创粉丝点击