B. Catching Cheaters

Problem - B - CodeforcesB. Catching Cheaters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

=========================================================================

反正CF给的式子能不用就不用,一定要转化。 4LCS -C -D= 2LCS - (C-LCS)- (D-LCS)

然后就套LCS的模板,也就是LCS增加1时,对答案的贡献为+2,否则对答案贡献为-1

注意LCS的模板 一个if就够了,不要手欠再写个else  ,否则会WA6 

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;


int dp[5010][5010];

int main()
{
    int n,m;
    cin>>n>>m;
    string s,t;
    cin>>s>>t;

    s=" "+s;
    t=" "+t;
    int ans=0;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(s[i]==t[j])
            {
                dp[i][j]=dp[i-1][j-1]+2;
            }

            dp[i][j]=max(dp[i][j],max(dp[i-1][j]-1,dp[i][j-1]-1));


           ans=max(ans,dp[i][j]);
        }
    }

    cout<<ans<<endl;


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值