Advanced Fruits-HDU1503
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/de39a7b916701d8560159bd819b465b6.png#pic_center)
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long
#define pii pair<int,int>
string s1,s2,s;
int l1,l2;
int dp[105][105];
stack<pii> sta;
void solve(){
l1 = s1.length(),l2 = s2.length();
s1 = " " + s1,s2 = " "+ s2,s = "";
for(int i = 1;i <= l1;++i)
for(int j = 1;j <= l2;++j)
if(s1[i] == s2[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i][j - 1],dp[i - 1][j]);
int x = l1,y = l2;
while(x >= 1 && y >= 1){
if(s1[x] == s2[y])
sta.push(pii(x,y)),--x,--y;
else if(dp[x - 1][y] >= dp[x][y - 1])
--x;
else
--y;
}
pii from,to;from.first = 1,from.second = 1;
while(!sta.empty()){
to = sta.top(),sta.pop();
s += s1.substr(from.first,to.first - from.first)
+ s2.substr(from.second,to.second - from.second)
+ s1[to.first];
from.first = to.first + 1,from.second = to.second + 1;
}
s += s1.substr(from.first,l1 + 1 - from.first)
+ s2.substr(from.second,l2 + 1 - from.second);
cout<<s<<"\n";
}
signed main(){
IOS;
while(cin>>s1>>s2)
solve();
return 0;
}