https://codeforces.ml/contest/1447/problem/D
You are given two strings AA and BB representing essays of two students who are suspected cheaters. For any two strings CC, DD we define their similarity score S(C,D)S(C,D) as 4⋅LCS(C,D)−|C|−|D|4⋅LCS(C,D)−|C|−|D|, where LCS(C,D)LCS(C,D) denotes the length of the Longest Common Subsequence of strings CC and DD.
You believe that only some part of the essays could have been copied, therefore you're interested in their substrings.
Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal S(C,D)S(C,D) over all pairs (C,D)(C,D), where CC is some substring of AA, and DD is some substring of BB.
If XX is a string, |X||X| denotes its length.
A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
A string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.
Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement.
You may wish to read the Wikipedia page about the Longest Common Subsequence problem.
Input
The first line contains two positive integers nn and mm (1≤n,m≤50001≤n,m≤5000) — lengths of the two strings AA and BB.
The second line contains a string consisting of nn lowercase Latin letters — string AA.
The third line contains a string consisting of mm lowercase Latin letters — string BB.
Output
Output maximal S(C,D)S(C,D) over all pairs (C,D)(C,D), where CC is some substring of AA, and DD is some substring of BB.
Examples
input
Copy
4 5 abba babab
output
Copy
5
input
Copy
8 10 bbbbabab bbbabaaaaa
output
Copy
12
input
Copy
7 7 uiibwws qhtkxcn
output
Copy
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|S(abb,abab)=(4⋅|abb|) - |abb||abb| - |abab||abab| = 4⋅3−3−4=54⋅3−3−4=5.
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e3+5;
const ll mod=1e9+7;
ll n,t,k,m,s,w,sum;
char x[maxn];
char y[maxn];
ll dp[maxn][maxn];
int main()
{
cin>>n>>m;
cin>>x;
cin>>y;
dp[0][0]=0;
ll ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(x[i]==y[j])
{
dp[i+1][j+1]=max(dp[i][j],0ll)+2;
}
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1])-1;
ans=max(ans,dp[i+1][j+1]);
}
}
cout<<ans<<endl;
return 0;
}