单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合部分合为一部分,例如beast和astonish,如果接成一条龙则变为beastonish,另外相邻的两部分不能存在包含关系,例如at和atide间不能相连。
分析:就是一道练习搜索熟练度的题==。
var v:array[1..20] of longint; s:array[1..20,1..20] of longint; a:array[1..20] of string; n,ans:longint; m:string; procedure init; var i:longint; begin readln(n); for i:=1 to n do readln(a[i]); readln(m); end; procedure print; begin write(ans); end; procedure hehe; var i,j,k,l,o,p:longint; begin for i:=1 to n do for j:=1 to n do begin k:=length(a[i]); o:=1; p:=k; for l:=1 to k do if copy(a[j],1,o)=copy(a[i],p,k) then begin s[j,i]:=o; break; end else begin inc(o); dec(p); if o>k then break; end; end; end; procedure dep(x,l:longint); var i:longint; begin if x>ans then ans:=x; for i:=1 to n do if (s[i,l]>0) and (v[i]<2) then begin inc(v[i]); dep(x+length(a[i])-s[i,l],i); dec(v[i]); end; end; procedure hehehe; var i:longint; begin for i:=1 to n do if a[i][1]=m then begin inc(v[i]); dep(length(a[i]),i); dec(v[i]); end; end; begin init; hehe; hehehe; print; end.