Codeforces Round #683 (Div. 2, by Meet IT) D. Catching Cheaters (DP)
作者:互联网
-
题意:给你两个字符串,每次取它们的子串C和D,然后求LCS,得到的贡献为\(4*LCS(C,D)-|C|-|D|\),求最大贡献.
-
题解:首先应该了解\(O(n^2)\)的LCS的dp写法,然后在此基础上稍加改动,对于子串\(C\)和\(D\),如果\(c[i]=d[j]\),那么他们的LCS应该\(+1\),长度也分别\(+1\),所以\(dp[i][j]=dp[i-1][j-1]+2\).而如果\(c[i]\)和\(d[j]\)不匹配,因为长度加一,所以贡献应该减一,但是由最大子段和,我们应使其不小于\(0\).
-
代码:
int n,m; string a,b; int dp[5010][5010]; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n>>m; cin>>a>>b; a=" "+a; b=" "+b; int ans=0; int cnt=0; rep(i,1,n){ //int cnt=0; rep(j,1,m){ if(a[i]==b[j]){ dp[i][j]=dp[i-1][j-1]+2; } else dp[i][j]=max({0,dp[i-1][j]-1,dp[i][j-1]-1}); ans=max(ans,dp[i][j]); } } cout<<ans<<'\n'; return 0; }
标签:5010,LCS,Cheaters,int,Codeforces,cin,Catching,ans,dp 来源: https://www.cnblogs.com/lr599909928/p/13990448.html