题目:
Description
设有A、B两个字符串,找出A、B共同子串,每个字符串无相同字符,可以不连续,但顺序不能颠倒。
Input
第一行字符串A
第二行字符串B
Output
最长公共子串的长度.
Sample Input
abcfbc
abfcab
Sample Output
4
作者思路:dp。
公式:f[i,j]:=max(f[i-1,j],f[i,j-1])
代码:
var st1,st2:string;
i,j:longint;
f:array[0..256,0..256] of longint;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end;
begin
readln(st1);
readln(st2);
for i:=1 to length(st1) do
for j:=1 to length(st2) do
if st1[i]=st2[j] then f[i,j]:=f[i-1,j-1]+1 else
f[i,j]:=max(f[i-1,j],f[i,j-1]);
write(f[length(st1),length(st2)]);
end.