【样例输入输出】
common.in
|
common.out
|
ABRACADABRA
ECADADABRBCRDARA
|
5
|
UPWJCIRUCAXIIRGL
SBQNYBSBZDFNEV
|
0
|
【数据范围】
30%的字符串长度不超过50
经典的dp
简便版状态转移方程如下
if a[i]=b[j] then f[i,j]=f[i-1,j-1]+1;
答案是 max(f[i,j]);
代码如下
var
s1,s2:ansistring;
f:array[0..4000,0..4000]of longint;
i,j,ans:longint;
begin
assign(input,'common.in');reset(input);
assign(output,'common.out');rewrite(output);
readln(s1);
readln(s2);
for i:=1 to length(s1) do
for j:=1 to length(s2) do
if s1[i]=s2[j] then f[i,j]:=f[i-1,j-1]+1;
for i:=1 to length(s1) do
for j:=1 to length(s2) do
if f[i,j]>ans then ans:=f[i,j];
write(ans);
close(input); close(output);
end.