• hihocoder1059 String Matching Content Length(带长度条件的最长公共子序列)


    给定只包含字母的两个字符串A,B,求A,B两个字符串的最长公共子序列,要求构成子序列的子串长度都必须大于等于3。
    比如"abcdefghijklmn"和"ababceghjklmn",其最长满足题意要求的子序列为"abcjklmn",其由公共子串"abc"和"jklmn"组成。

    思路:

    http://hihocoder.com/discuss/question/2111

    /* ***********************************************
    Author        :devil
    Created Time  :2016/5/16 22:11:25
    ************************************************ */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <cmath>
    #include <stdlib.h>
    using namespace std;
    int f[2010][2010],dp[2010][2010][2];
    char s1[2010],s2[2010];
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%s%s",s1,s2);
        int n=strlen(s1),m=strlen(s2);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(s1[i-1]==s2[j-1])
                    f[i][j]=f[i-1][j-1]+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                dp[i][j][1]=0;
                if(f[i][j]>=3)
                {
                    dp[i][j][1]=max(dp[i][j][1],dp[i-3][j-3][0]+3);
                    if(f[i][j]>3) dp[i][j][1]=max(dp[i][j][1],dp[i-1][j-1][1]+1);
                }
                dp[i][j][0]=max(dp[i-1][j][0],max(dp[i][j-1][0],dp[i][j][1]));
            }
        }
        printf("%d
    ",dp[n][m][0]);
        return 0;
    }
  • 相关阅读:
    【python刷题】前缀和
    【python刷题】数组链表去重
    【python刷题】滑动窗口法
    【python刷题】二分查找
    【python刷题】广度优先搜索(BFS)
    【python刷题】回溯算法(深度优先搜索DFS)
    机器学习十讲-第三讲分类
    数学知识-质数&约数
    树与图的DFS与BFS
    桥梁保护与监控-开发进度(二)
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/5499721.html
Copyright © 2020-2023  润新知