1 program no;
2 uses math;
3 const
4 inf=‘test.in‘;
5 outf=‘test.out‘;
6 var
7 a:array[1..20] of string;
8 i,j,n,tot,ans:longint;
9 f:array[1..20,1..20] of longint;
10 ex:array[1..20] of longint;
11 tt:char;
12
13 procedure compare(aa,bb:longint);
14 var
15 i,j,lena,lenb,k:longint;
16 begin
17 lenb:=length(a[bb]);
18 lena:=length(a[aa]);
19 k:=maxlongint;
20 for j:= lena downto 1 do
21 if a[aa,j]=a[bb,1] then
22 if copy(a[aa],j,lena-j+1)=copy(a[bb],1,lena-j+1) then
23 if (lena-j+1=min(lena,lenb)) then //如果有包含关系
24 begin if aa<>bb then exit; end //排除自己接自己
25 else begin
26 k:=min(k,lena-j+1);
27 break;//优化
28 end;
29
30 if k<>maxlongint then
31 f[aa,bb]:=lenb-k;//记录延长的长度
32 end;
33
34 procedure search(i:Longint); //dfs
35 var
36 j:longint;
37 begin
38 for j:= 1 to n do
39 if ex[j]>-2 then //如果还有可以用的次数
40 if f[i,j]<>0 then //如果不是白接
41 begin
42 // writeln(i,‘ ‘,j,‘ ‘,f[i,j]);
43 tot:=tot+f[i,j];
44 dec(ex[j]);
45 search(j);
46 tot:=tot-f[i,j];
47 inc(ex[j]);
48 end;
49 if ans<tot then ans:=tot;
50 end;
51
52 begin
53 //assign(input,inf); assign(output,outf);
54 reset(input); rewrite(output);
55
56 readln(n);
57 for i:= 1 to n do
58 readln(a[i]);
59 for i:= 1 to n do
60 for j:= 1 to n do
61 compare(i,j);
62 readln(tt);
63 for i:= 1 to n do //枚举开头的串
64 if tt=a[i,1] then
65 begin
66 fillchar(ex,sizeof(ex),0);
67 tot:=length(a[i]);
68 dec(ex[i]);
69 search(i);
70 end;
71 writeln(ans);
72 close(input); close(output);
73 end.