String Matching Content Length

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
We define the matching contents in the strings of strA and strB as common substrings of the two strings. There are two additional restrictions on the common substrings.

The first restriction here is that every common substring’s length should not be less than 3. For example:

strA: abcdefghijklmn
strB: ababceghjklmn

The matching contents in strA and strB are substrings (“abc”, “jklmn”). Note that though “e” and “gh” are common substrings of strA and strB, they are not matching content because their lengths are less than 3.

The second restriction is that the start indexes of all common substrings should be monotone increasing. For example:

strA: aaabbbbccc
strB: aaacccbbbb

The matching contents in strA and strB are substrings (“aaa”, “bbbb”). Note that though “ccc” is common substring of strA and strB and has length not less than 3, the start indexes of (“aaa”, “bbbb”, “ccc”) in strB are (0, 6, 3), which is not monotone increasing.

输入
Two lines. The first line is strA and the second line is strB. Both strA and strB are of length less than 2100.

输出
The maximum length of matching contents (the sum of the lengths of the common substrings).

样例输入
abcdefghijklmn
ababceghjklmn
样例输出
8

#include "iostream"
#include "string.h"
#include "stdlib.h"
#define max(a, b) a > b ? a : b
using namespace std;

char strA[2101];
char strB[2101];
int f[2101][2101]; //f[3][4]表示a[1..3]和b[1..4]的公共后缀的长度
int dp[2101][2101];  
int dp1[2101][2101]; //f[i][j]>3时, 枚举分割长度
int lenA, lenB;

void solvF()
{
    int i, j;
    for(i=1; i<=lenA; i++)
        for(j=1; j<=lenB; j++)
            if(strA[i-1] == strB[j-1])
                f[i][j] = f[i-1][j-1] + 1;
            else
                f[i][j] = 0;
}

void DP()
{
    int i, j;
    for(i=1; i<=lenA; i++)
        for(j=1; j<=lenB; j++)
        {
            dp1[i][j] = 0;
            if(f[i][j] >= 3)
                dp1[i][j] = max(dp1[i][j], dp[i-3][j-3] + 3);    // 以长度3为分割
            if(f[i][j] > 3)    //按照dp1[i-1][j-1]的分割方式分割,即直接将(i,j)接在(i-1,j-1)后面
                dp1[i][j] = max(dp1[i][j], dp1[i-1][j-1] + 1);
            dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
            dp[i][j] = max(dp[i][j], dp1[i][j]);
        }   
}

int main()
{
    memset(dp, 0, sizeof(dp));
    memset(f, 0, sizeof(f));
    char a[2101], b[2101];
    cin >> strA;
    cin >> strB;
    lenA = strlen(strA);
    lenB = strlen(strB);
    solvF();
    DP();
    cout << dp[strlen(strA)][strlen(strB)];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值