#最长公共子序列
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int c[100][100];
int b[100][100];
void Length(string x,string y,int m,int n)
{
memset(c,0,sizeof(c));
memset(b,0,sizeof(b));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(x[i-1]==y[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=1;
}
else if(c[i][j-1]>c[i-1][j])
{
c[i][j]=c[i][j-1];
b[i][j]=2;
}
else
{
c[i][j]=c[i-1][j];
b[i][j]=3;
}
}
}
}
void LCS(int i,int j,string x)
{
if(i==0||j==0)
return;
if(b[i][j]==1)
{
LCS(i-1,j-1,x);
cout<<x[i-1];
}
else if(b[i][j]==2)
{
LCS(i,j-1,x);
}
else
LCS(i-1,j,x);
}
int main()
{
string x="abcbdab";
string y="bdcaba";
int m=7;int n=6;
Length(x,y,7,6);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cout<<c[i][j]<<'\t';
}
cout<<endl;
}
cout<<endl;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
cout<<b[i][j]<<'\t';
}
cout<<endl;
}
LCS(7,6,x);
return 0;
}