|
板凳
楼主 |
发表于 2018-5-26 21:18:50
|
只看该作者
17年前我的程序,2001年9月写的 ,好遥远呀!
- const
- maxn=20;
- var
- s : array[1..maxn] of string;
- head : char;
- best,i,n : integer;
- add : array[1..maxn,1..maxn] of integer;
- used : array[1..maxn] of integer;
- procedure calcadd;
- var
- i,j,k,t,min:integer;
- ok:boolean;
- begin
- for i:=1 to n do
- for j:=1 to n do
- begin
- {取s[i]和s[j]中较小值}
- if length(s[i])<length(s[j]) then
- min:=length(s[i])
- else min:=length(s[j]);
- for k:=1 to min-1 do
- begin
- {check}
- ok:=true;
- for t:=1 to k do
- if s[j,t]<>s[i,length(s[i])-k+t] then
- begin
- ok:=false;
- break;
- end;
- {OK指能否连接上}
- if ok then break;
- end;
- if ok then
- add[i,j]:=length(s[j])-k
- else
- add[i,j]:=0;
- end;
- {计算连接后的长度}
- end;
- procedure search(last,len:integer);
- var
- i:integer;
- begin
- if len>best then best:=len;
- for i:=1 to n do
- if (add[last,i]>0)and(used[i]<2) then
- begin
- inc(used[i]);
- search(i,len+add[last,i]);
- dec(used[i]);
- end;
- end;
- begin
- readln(n);
- for i:=1 to n do
- readln(s[i]);
- readln(head);
- calcadd;
- best:=0;
- fillchar(used,sizeof(used),0);
- for i:=1 to n do
- if s[i,1]=head then
- begin
- used[i]:=1;
- search(i,length(s[i]));
- used[i]:=0;
- end;
- writeln(best);
- end.
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|