hiho一下 第六十周 题目1 : String Matching Content Length dp 最长公共子序列

本文主要是介绍hiho一下 第六十周 题目1 : String Matching Content Length dp 最长公共子序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目1 : 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
题意,要求最大的公共子序列,要求共公子序列的每一个子串都要是大于3的

类似于经典的最长公共子序列的做法。

dp[i][j][3]第一个串s1前i位  第二个串s2前j位,最后一个子串长度为0 1 2 3(>=3)的最长公共子序列。

状态转移

1,dp[i][j][0]转移到dp[i-1][j][0],dp[i-1][j][3] dp[i][j-1][0] dp[i][j-1][3];也就是第s1[i] 与s2[j]不匹配,直接转移到前一位就可以了。

2.s1[i] == s2[j]则,dp[i][j][k]可以转移到dp[i][j][k-1];也就是同是加一位。

初始化的时候,要注意,dp[0][i],dp[i][0]都为0;

由于有n * n个状态,状态转移为o(1)所有总的复杂度为O(n *n);

其次,给些测试数据

abcdefghijklmn
ababceghjklmn


aaabbbbccc
aaacccbbbb


aaa
aaa
ab
ab
aaaab
baaaa


abcba
bcbaa


babad
babacabad


abcdef
abcxcdef

#define N 2205
#define M 100005
#define maxn 2000005
#define MOD 1000000000000000007
int n,m,dp[N][N][4];
char s1[N],s2[N];
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);while(SS(s1)!=EOF){SS(s2);n = strlen(s1);m = strlen(s2);FI(n) FJ(m) For(k,0,4) dp[i][j][k] = -1;FI(n+1)dp[0][i][0] = dp[i][0][0] =0;For(i,1,n+1){For(j,1,m+1){dp[i][j][0] = max(dp[i][j][0],dp[i][j-1][0]);dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][0]);dp[i][j][0] = max(dp[i][j][0],dp[i][j-1][3]);dp[i][j][0] = max(dp[i][j][0],dp[i-1][j][3]);if(s1[i-1] == s2[j-1]){For(k,1,4)if(dp[i-1][j-1][k-1] >= 0)dp[i][j][k] = max(dp[i][j][k],dp[i-1][j-1][k-1] + 1);if(dp[i-1][j-1][3] >= 0)dp[i][j][3] = max(dp[i][j][3],dp[i-1][j-1][3] + 1);}}}printf("%d\n",max(0,max(dp[n][m][0],dp[n][m][3])));}//fclose(stdin);//fclose(stdout);return 0;
}


这篇关于hiho一下 第六十周 题目1 : String Matching Content Length dp 最长公共子序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/988670

相关文章

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

poj3261(可重复k次的最长子串)

题意:可重复k次的最长子串 解题思路:求所有区间[x,x+k-1]中的最小值的最大值。求sa时间复杂度Nlog(N),求最值时间复杂度N*N,但实际复杂度很低。题目数据也比较水,不然估计过不了。 代码入下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring

poj1330(LCA最近公共祖先)

题意:求最近公共祖先 思路:之前学习了树链剖分,然后我就用树链剖分的一小部分知识就可以解这个题目了,记录每个结点的fa和depth。然后查找时,每次将depth大的结点往上走直到x = y。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b