zoukankan      html  css  js  c++  java
  • 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
    ",ma);
        //system ("pause");
        return 0;
    }
  • 相关阅读:
    不规范的json文档 转化成 java 对象的处理
    财经接口
    Back-off pulling image "registry.access.redhat.com/rhel7/pod-infrastructure:latest
    VMware Workstation 14 Pro永久激活密钥
    Angular2入门:TypeScript的装饰器
    Angular2入门:TypeScript的模块
    Angular2入门:TypeScript的类
    51nod“省选”模测第二场 B 异或约数和(数论分块)
    51nod1238 最小公倍数之和 V3(莫比乌斯反演)
    cf1139D. Steps to One(dp)
  • 原文地址:https://www.cnblogs.com/dyzll/p/5676398.html
Copyright © 2011-2022 走看看