题目传送门
类比最长公共子序列,(f_{i,j,k})表示到(A_i,B_j,C_k)的最大答案,然后跟最长公共自序列一样转移即可
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string a,b,c,ans[105][105][105];
int f[105][105][105];
inline string dp(string x,string y,string z) {
memset(f,0,sizeof(f));
for(int i = 1;i <= x.length(); i++)
for(int j = 1;j <= y.length(); j++)
for(int k = 1;k <= z.length(); k++) {
if(x[i-1] == y[j-1] && x[i-1] == z[k-1]) {
f[i][j][k] = f[i-1][j-1][k-1] + 1;
ans[i][j][k] = ans[i-1][j-1][k-1] + x[i-1];
}
else {
f[i][j][k] = max(f[i-1][j][k],max(f[i][j-1][k],f[i][j][k-1]));
if(f[i-1][j][k] >= f[i][j-1][k] && f[i-1][j][k] >= f[i][j][k-1])
ans[i][j][k] = ans[i-1][j][k];
else
if(f[i][j-1][k] >= f[i][j][k-1])
ans[i][j][k] = ans[i][j-1][k];
else
ans[i][j][k] = ans[i][j][k-1];
}
}
return ans[x.length()][y.length()][z.length()];
}
int main() {
cin >> a >> b >> c;
a = dp(a,b,c);
cout << a;
return 0;
}