原题链接; http://codeforces.com/contest/1447/problem/D
测试样例
input
4 5
abba
babab
output
5
input
8 10
bbbbabab
bbbabaaaaa
output
12
input
7 7
uiibwws
qhtkxcn
output
0
Note
For the first case:
abb from the first string and abab from the second string have LCS equal to abb.
The result is S(abb,abab)=(4⋅|abb|) - |abb| - |abab| = 4⋅3−3−4=5.
题意: 给你两个字符串。认定
,其中
为
的子串,
为
的子串。LCS代表的是求
和
的最长公共子序列。
代表求字符串的长度。现在你需要最大化
。
解题思路: 最长公共子序列的变种题。如果我们了解过这阵求最长公共子序列的题,那么这个题就好办了,我们要清楚贡献,。那么此刻我们如果都往后移的话那么总得分是加
。而如果不相等大的话,那么我们这个状态是可以通过上一个状态转移过来的,即
,那么此时LCS没变,而长度增
。故总得分减
。其余的即是按动态规划求解最长公共子序列一样做,只不过这个题需要实时保存最大值。具体看代码。
AC代码
/*
*
*/
//POJ不支持
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=5500;//限定值。
typedef long long ll;
int n,m;
char a[maxn],b[maxn];
int dp[maxn][maxn];
int main(){
while(cin>>n>>m){
cin>>(a+1)>>(b+1);
memset(dp,0,sizeof(dp));
int maxx=0;
rep(i,1,n){
rep(j,1,m){
if(a[i]==b[j]){
dp[i][j]=max(0,dp[i-1][j-1])+2;
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1])-1;
}
maxx=max(maxx,dp[i][j]);
}
}
cout<<maxx<<endl;
}
return 0;
}