hdoj-1159-Common Subsequence【动态规划求最长公共子序列】

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 28152 Accepted Submission(s): 12556

Problem 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.

The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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

Recommend

Ignatius | We have carefully selected several similar problems for you:
1087 1058 1069 1421 1160

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s1[1000],s2[1000];
int c[1001][1001];
int main(){
	while(~scanf("%s%s",s1,s2)){
		int len1=strlen(s1),len2=strlen(s2);
		memset(c,0,sizeof(c));
		for(int i=0;i<len1;++i){
			for(int j=0;j<len2;++j){
				if(s1[i]==s2[j]){
					c[i+1][j+1]=c[i][j]+1;
				}
				else c[i+1][j+1]=max(c[i][j+1],c[i+1][j]);
			}
		}
		printf("%d\n",c[len1][len2]);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 08:17:25

hdoj-1159-Common Subsequence【动态规划求最长公共子序列】的相关文章

HDU 1159 Common Subsequence (动规+最长公共子序列)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22698    Accepted Submission(s): 9967 Problem Description A subsequence of a given sequence is the given sequence with some el

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

动态规划求最长公共子序列问题

#include<iostream> #include<string> #include<vector> using namespace std; //找两个子串str1,str2的最长公共子串substr void findLongestSubString(string &str1, string &str2, string &substr){ if (str1.empty() || str2.empty()){ return; } //定义二

HDU 1243 反恐训练营 (动态规划求最长公共子序列)

反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3040    Accepted Submission(s): 693 Problem Description 当今国际反恐形势很严峻,特别是美国"9.11事件"以后,国际恐怖势力更是有恃无恐,制造了多起骇人听闻的恐怖事件.基于此,各国都十分担心恐怖势力会对本国社会造

九度OJ 1042 Coincidence (动态规划求最长公共子序列)

题目1042:Coincidence 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1689 解决:898 题目描述: Find a longest common subsequence of two strings. 输入: First and second line of each input case contain two strings of lowercase character a-z. There are no spaces before, inside or aft

POJ - 1458 Common Subsequence (LCS最长公共子序列)

题意: 给出两个字符串,求出最长的公共子序列大小. 思路: 算是最经典的LCS问题吧. 设 \(X=(x_1,x_2,.....x_n) 和 Y=(y_1,y_2,.....y_m)\) 是两个序列,将 X 和 Y 的最长公共子序列记为\(lcs(X,Y)\) ,找出\(lcs(X,Y)\)就是一个最优问题 然后我们需要将其分解成子问题并求出子问题的最优解: (寻找子问题来推出当前问题,正是寻找状态转移方程最重要的一步) 1)如果 \(x_n=y_m\),即X的最后一个元素与Y的最后一个元素相同

HDU 1159 Common Subsequence (动态规划、最长公共子序列)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 55301    Accepted Submission(s): 25537 Problem Description A subsequence of a given sequence is the given sequence with some el

(hdu step 3.2.2)Common Subsequence(简单dp:求最长公共子序列的长度)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 976 Accepted Submission(s): 538   Probl

HDU 1159 Common Subsequence 动态规划

2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中大的公共子序列,每个样例输出一个数: 最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为: 子串是串的一个连续的部分,子序列则是从不改变序列的顺序,而从序列中去掉任意的元素而获得新的序列: 也就是说