/*最长公共子序列-可不连续*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=2000;
int dp[maxn][maxn];
int fa[maxn][maxn];//记录是dp[i][j]是通过哪个子问题求出
/*递归式:
dp[i][j] 表示记录a[i] b[j]的LSC的长度
a[i]==b[j] dp[i-1][j-1]+1;
a[i]!=b[j] max(dp[i-1][j],dp[i][j-1]);

时间复杂度:O(m+n);
*/
void pri(string a,int x,int y)
{
if(x==0||y==0)
return ;
if(fa[x][y]==0)
{
pri(a,x-1,y-1);
printf("%c",a[x-1]);
}
else if(fa[x][y]==1)
pri(a,x-1,y);
else
pri(a,x,y-1);
}
int main()
{
string a,b;
while(cin>>a>>b){//
int len_a=a.size();
int len_b=b.size();
memset(dp,0,sizeof(dp));

for(int i=1;i<=len_a;i++){
for(int j=1;j<=len_b;j++){
if(a[i-1]==b[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
fa[i][j]=0;
}
else
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
if(dp[i-1][j]>=dp[i][j-1])
fa[i][j]=1;
else
fa[i][j]=-1;
}
}
}
cout<<dp[len_a][len_b]<<endl;

cout<<"输出路径显示"<<endl;
for(int i=0;i<=len_a;i++)
{
for(int j=0;j<=len_b;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}

cout<<"回溯输出最长"<<endl;
pri(a,len_a,len_b);
}
return 0;