ab fk ce tt ak bt < b
0 0 0 0 0 0 0
ab 0 1 1 1 1 1 1 输出 ab
bd 0 1 1 1 1 1 1
fk 0 1 2 2 2 2 2 输出 fk
ce 0 1 2 3 3 3 3 输出 ce
ak 0 1 2 3 3 4 4 输出 ak
bt 0 1 2 3 3 4 5 输出 bt
cv 0 1 2 3 3 4 5
^
a
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#define ll long long
using namespace std;
string a[105],b[105];
int dp[105][105],n,m;
void dfs(int n,int m)//打印序列
{
if(dp[n][m]==0)
return;
else if(dp[n][m]==dp[n-1][m-1]+1&&a[n]==b[m])
{
dfs(n-1,m-1);
cout << a[n] << " ";
}
else if(dp[n][m]==dp[n][m-1])
dfs(n,m-1);
else
dfs(n-1,m);
}
int main()
{
while(cin >> a[1])
{
n=1,m=0;
while(cin >> a[++n],a[n]!="#");
n--;
while(cin >> b[++m],b[m]!="#");
m--;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i]==b[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
dfs(n,m);
cout << endl;
}
return 0;
}