poj1458 dp入门

Common Subsequence

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37551   Accepted: 15023

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc         abfcab
programming    contest
abcd           mnp

Sample Output

4
2
0

Source

Southeastern Europe 2003

入门dp,主要是理解动归的思考方式,把串变断,比如先确定两个串从头开始的第一个字符相同和不相同两种状态下对后面有什么影响,然后想想怎么描述两个串公共子序列这个状态,我们这里Maxsum[i][j]表示(0~i)和(0~j)两个串当前情况下最长公共子序列的长度,考虑最小子问题情况,第一个字符相同,则Maxsum[i+1][j+1] = Maxsum[i][j]+1;

考虑初始状态,很容易想到,Maxsum[0][len1]和Maxsum[len2][0]是不可能有公共子序列的,为0,。

Maxsum(i,j)不会比Maxsum(i,j-1)
和Maxsum(i-1,j)两者之中任何一个小,也不会比两者都大。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int main()
 6 {
 7     char str1[1001],str2[1001];
 8     while(scanf("%s%s",str1,str2)!=EOF)
 9     {
10         int len1 = strlen(str1);
11         int len2 = strlen(str2);
12         int Maxsum[1001][1001];  //Maxsum[i][j] ,i表示长度为i的串一,j表示长度为j的串二,Maxsum[i][j]两串最大公共子序列
13         for(int i=0;i<len1;i++)
14         {
15             Maxsum[i][0] = 0;
16         }
17         for(int j=0;j<len2;j++)
18         {
19             Maxsum[j][0] = 0;
20         }
21
22         for(int i=0;i<len1;i++)
23             for(int j=0;j<len2;j++)
24                 if(str1[i]==str2[j])
25                     Maxsum[i+1][j+1] = Maxsum[i][j] +1;
26                 else{
27                     Maxsum[i+1][j+1] = max(Maxsum[i][j+1],Maxsum[i+1][j]);
28                 }
29         printf("%d\n",Maxsum[len1][len2]);
30     }
31     return 0;
32 }

poj1458 dp入门

时间: 2024-11-08 17:40:20

poj1458 dp入门的相关文章

HDU 2084 数塔(简单DP入门)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 41852    Accepted Submission(s): 24820 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

hdu3507之斜率优化DP入门

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 4780    Accepted Submission(s): 1437 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

[poj2342]Anniversary party树形dp入门

题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max(dp[j][0],dp[j][1]); 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<iostream> 6

HDU 2089 不要62 数位DP入门

Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众.不吉利的数字为所有含有4或62的号码.例如:62315 73418 88914都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列.你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多

poj 3254 Corn Fields 状压dp入门

// poj3254 状压dp入门 // dp[i][S]表示前i行第i行状态为S时放牧的情况 // S为十进制的二进制数形式如5为101表示第一列和第三列放牧的方法数 // 首先dp[0][S]合法的话就是1 // 状态转移方程为dp[i][S] = sigma(dp[i-1][V])(S与V是合法状态) // 最后的结果就是dp[n][S](S为所有的合法状态) // // 刚开始十分傻x的dp[0][S]置为get(S),...get(S)是其中1的个数 // 这又不是求放羊的数量,这是方

poj1458——dp,lcs

poj1458——dp,lcs Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40529   Accepted: 16351 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence

poj 2411 Mondriaan&#39;s Dream 状压dp入门

题意: 求h*w的矩形被1*2的小矩形覆盖的方案数. 分析: 状压dp入门,<挑战程序设计竞赛>上讲的很好,好几天才看懂. 代码: #include <iostream> using namespace std; __int64 ans[16][16]; int n,m; __int64 dp[2][1<<16]; __int64 solve() { int i,j,used; memset(dp,0,sizeof(dp)); __int64 *crt=dp[0],*n