公共子串-SSL 1463

Description

设有A、B两个字符串,找出A、B共同子串,每个字符串无相同字符,可以不连续,但顺序不能颠倒。

Input

第一行字符串A 
第二行字符串B 

Output

最长公共子串的长度.

Sample Input


abcfbc
abfcab
Sample Output


4

题解:这道题可以用动规,用顺推的方法。
     if s1[i]=s2[j] then t[i,j]:=t[i-1,j-1]+1
                  else t[i,j]:=zdzc(t[i-1,j],t[i,j-1]);
     t[i]表示子串的长度。

const maxn=255;
var s1,s2:string;
    t:array[0..255,0..255] of longint;
    i,j:longint;

function zdzc(x,y:longint):longint;
begin
  if x>y then exit(x) else exit(y);
end;

begin
  readln(s1);
  readln(s2);
  for i:=1 to length(s1) do
   for j:=1 to length(s2) do
   if s1[i]=s2[j] then t[i,j]:=t[i-1,j-1]+1
                  else t[i,j]:=zdzc(t[i-1,j],t[i,j-1]);
   write(t[length(s1),length(s2)]);
end.

你可能感兴趣的:(公共子串-SSL 1463)