String Matching Content Length

hihocoder #1059 :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
分析:与LCS不同的是加了每个子串长度不小于3,所以状态转移方程有
    dp[i+1][j+1]=max({dp[i+1][j+1],dp[i][j]+ok[i-1][j-1],dp[i-2][j-2]+3}),当有连续3字符形成,且ok数组判断a,b字符串前一个字符是否有连续3字符形成。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=2e3+20;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,ma,dp[maxn][maxn],len1,len2,ok[maxn][maxn];
char a[maxn],b[maxn];
int main()
{
    int i,j,k,t;
    scanf("%s%s",a,b);
    len1=strlen(a),len2=strlen(b);
    rep(i,2,len1-1)rep(j,2,len2-1)
    {
        dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
        if(a[i]==b[j]&&a[i-1]==b[j-1]&&a[i-2]==b[j-2])
        {
            dp[i+1][j+1]=max({dp[i+1][j+1],dp[i][j]+ok[i-1][j-1],dp[i-2][j-2]+3});
            ok[i][j]=1;
        }
        ma=max(ma,dp[i+1][j+1]);
    }
    printf("%d\n",ma);
    //system ("pause");
    return 0;
}

 

posted @ 2016-07-16 14:58  mxzf0213  阅读(262)  评论(0编辑  收藏  举报