LCS
题解:
对于每个字符有两种情况,等,或不等,而对于不等我们又有两种选择,用1号序列,或用2号序列,对每次操作得到的数据取最大值即可
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,dp[1005][1005],res;
char a[1005],b[1005];
int main(){
scanf("%s",a);
scanf("%s",b);
n=strlen(a);
m=strlen(b);
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][j-1],dp[i-1][j]);
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) res=max(res,dp[i][j]);
printf("%d",res);
return 0;
}