//15.4最长公共子序列
#include <iostream>
#include <string>
using namespace std;
int c[8][7];
int b[8][7];
#define x 8
#define y 7
void Lcs_Length(char s1[x],char s2[y])//求解最长公共序列
{
int i,j;
for(i=1;i<x;++i)
c[i][0]=0;
for(j=0;j<y;++j)
c[0][j]=0;
for(i=1;i<x;++i)
for(j=1;j<y;++j)
{
if(s1[i]==s2[j])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=1;
}
else
if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=2;
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=3;
}
}
}
void Print_Lcs(int b[][7],string s1,int i,int j)//递归输出最长公共序列
{
if(i==0||j==0)
return;
if(b[i][j]==1)
{
Print_Lcs(b,s1,i-1,j-1);
cout<<s1[i];
}
else
if(b[i][j]==2)
Print_Lcs(b,s1,i-1,j);
else
Print_Lcs(b,s1,i,j-1);
}
void main()
{
char s1[x]={' ','A','B','C','B','D','A','B'},s2[y]={' ','B','D','C','A','B','A'};//第一个字符为空字符
Lcs_Length(s1,s2);
Print_Lcs(b,s1,7,6);
cout<<endl;
}