codeforces1446B. Catching Cheaters

time limit per test1 second memory limit per test256 megabytes
inputstandard input outputstandard output You are given two strings A
and B representing essays of two students who are suspected cheaters.
For any two strings C, D we define their similarity score S(C,D) as
4⋅LCS(C,D)−|C|−|D|, where LCS(C,D) denotes the length of the Longest
Common Subsequence of strings C and D.

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) over all pairs (C,D), where C is
some substring of A, and D is some substring of B.

If X is a string, |X| denotes its length.

A string a is a substring of a string b if a can be obtained from b by
deletion of several (possibly, zero or all) characters from the
beginning and several (possibly, zero or all) characters from the end.

A string a is a subsequence of a string b if a can be obtained from b
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 n and m
(1≤n,m≤5000) — lengths of the two strings A and B.

The second line contains a string consisting of n lowercase Latin
letters — string A.

The third line contains a string consisting of m lowercase Latin
letters — string B.

Output Output maximal S(C,D) over all pairs (C,D), where C is some
substring of A, and D is some substring of B.

Examples inputCopy 4 5 abba babab outputCopy 5 inputCopy 8 10 bbbbabab
bbbabaaaaa outputCopy 12 inputCopy 7 7 uiibwws qhtkxcn outputCopy 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.

由式s=4*lcs(s1,s2)-|s1|-|s2|
可知每当s1与s2有一处相同,Δs=4-1-1=2;
那么可以得到下面这个式子
if(s1[i]==s2[j])dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+2)
我们以dp[i][j]记录s1的i位置和s2的j位置的答案
可以知道
dp[i+1][j]=max(dp[i+1][j],dp[i][j]-1)
dp[i][j+1]=max(dp[i][j+1],dp[i][j]-1)

最后找出最大值就行了

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> Int;
int n,m;
string s1,s2;
int dp[5001][5001];
signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    cin>>n>>m;
    cin>>s1>>s2;
    int ans=0;
    int cur1=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
           dp[i+1][j]=max(dp[i+1][j],dp[i][j]-1);
           dp[i][j+1]=max(dp[i][j+1],dp[i][j]-1);
           if(s1[i]==s2[j]){
            dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+2);
           }
        }
    }

    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++)ans=max(ans,dp[i][j]);
    }
    cout<<ans<<endl;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值