题意:中文题。
分析:基础dp,最长公共子序列,再加个输出过程即可。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1005;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const int INF=1000000010;
const ll MAX=1000000000000;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
char a[N],b[N],s[N];
int dp[N][N];
int main()
{
int i,j,k,lena,lenb;
scanf("%s", a);
scanf("%s", b);
lena=strlen(a);lenb=strlen(b);
for (i=1;i<=lena;i++)
for (j=1;j<=lenb;j++)
if (a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
k=dp[lena][lenb];s[k]='\0';
while (lena&&lenb) {
if (dp[lena][lenb]==dp[lena][lenb-1]) lenb--;
else if (dp[lena][lenb]==dp[lena-1][lenb]) lena--;
else {
s[--k]=a[lena-1];lena--;lenb--;
}
}
printf("%s\n", s);
return 0;
}