单词接龙 Pascalvar a:array[0..20] of string; b,c:array[0..20] of shortint; l,n:shortint; s:string; ss,max:integer;procedure link(head:string);var k,j,i:byte; s1,s2:string;begin for i:=1 to n do if c[i]=b[i] then k:=b[i]-1 else k:=
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/25 08:24:51
单词接龙 Pascalvar a:array[0..20] of string; b,c:array[0..20] of shortint; l,n:shortint; s:string; ss,max:integer;procedure link(head:string);var k,j,i:byte; s1,s2:string;begin for i:=1 to n do if c[i]=b[i] then k:=b[i]-1 else k:=
单词接龙 Pascal
var a:array[0..20] of string;
b,c:array[0..20] of shortint;
l,n:shortint; s:string; ss,max:integer;
procedure link(head:string);
var k,j,i:byte; s1,s2:string;
begin
for i:=1 to n do
if c[i]=b[i] then k:=b[i]-1
else k:=length(head);
s1:=''; s2:='';
for j:=1 to k do
begin
s1:=head[length(head)+1-j]+s1;
s2:=s2+s[j];
if s1=s2 then
begin
ss:=ss+b[i]-j; if ss>max then max:=ss;
inc(c[i]);
link(a[i]);
dec(c[i]);
ss:=ss+j-b[i];
end;
end;
end;
end;
begin
assign(input,'c.in'); reset(input);
assign(output,'c.out'); rewrite(output);
readln(n);max:=0;
for l:=1 to n do
begin
readln(s);
a[l]:=s; b[l]:=length(s); c[l]:=0;
end;
readln(s); ss:=length(s);
link(s);
writeln(max);
close(input); close(output);
end.
把这个程序的思路清楚的说一下.
单词接龙 Pascalvar a:array[0..20] of string; b,c:array[0..20] of shortint; l,n:shortint; s:string; ss,max:integer;procedure link(head:string);var k,j,i:byte; s1,s2:string;begin for i:=1 to n do if c[i]=b[i] then k:=b[i]-1 else k:=
a[i]用来存字符串;b[i]存字符串的相应长度;
c[i]存字符出现的次数(最多2次;作为procedure link中的判断条件);
ss是存当前字符串的总长;max是求的字符串最长长度;
link中,head是当前搜索到的字符串,接着循环找下一个字串;
k是当前字串head与搜索到的字串s(a[i])相比得到的最短长度-1
(也是能有相同字符的最长长度);
j那个循环是寻找head与s的相同字符;
s1,s2分别记录head与s的部分字符串;
s1=s2,则继续找下一个字符串;
大概是这意思
递归搜索...
你会理解的
主要是哪一步不懂?
找到一个再搜下去