POJ 1458 Common Subsequence(动态规划)

10人阅读 评论(0) 收藏 举报
分类:

点我,我知道题目

Common Subsequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 58460 Accepted: 24411

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

解题思路:



在提交的时候出现了runtime error,Time Limit Exceeded错误,分别是编写的时候出现错误导致程序中途崩溃,虽然在编译器上一切正常,还有是缺少程序终止条件,导致程序一直在运行,无法结束,导致超时。下面链接介绍超时和运行时间错误的博客。runtime errorTime Limit Exceeded

#include<iostream>
#include<cstring>
using namespace std;
#define MAXN 1005
char sz1[MAXN],sz2[MAXN];
int MaxLen[MAXN][MAXN];
int main(){
	while(scanf("%s%s",sz1,sz2) != EOF){
		int length1 = strlen(sz1);
		int length2 = strlen(sz2);
		int i,j;
		for(i = 0; i <= length1; i++)
			MaxLen[i][0] = 0;
		for(i = 0; i <= length2; i++)
			MaxLen[0][i] = 0;
		for(i = 1; i <= length1; i++)
			for(j = 1; j <= length2; j++){
				if(sz1[i-1] == sz2[j-1])
					MaxLen[i][j] = MaxLen[i-1][j-1] + 1;
				else
					MaxLen[i][j] = max(MaxLen[i][j-1],MaxLen[i-1][j]);
			}
		printf("%d\n",MaxLen[length1][length2]);
	}
	return 0;
}
本题的分析由上课老师给的讲解,本处作为记录,方便之后的进一步学习。


查看评论

详解<K's 50 PV> (下篇)

 Explanatory Notes On 详解 Revision 1.0[下篇]by  Kingofark [注]:Ks 50 PV>是《Kingofarks 50 Points of View A...
  • kingofark
  • kingofark
  • 2001-11-09 16:14:00
  • 1252

POJ 1458 Common Subsequence 最长公共子序列

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3730...
  • briup_acmer
  • briup_acmer
  • 2014-07-12 23:19:54
  • 320

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

Sample Input abcfbc abfcab programming contest abcd mnp   Sample Output 4 2 0 #...
  • onthewaytotop
  • onthewaytotop
  • 2016-07-19 20:44:51
  • 76

hdu1159 Common Subsequence 最长公共子序列 模板题 入门题

Common SubsequenceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
  • cao2219600
  • cao2219600
  • 2018-04-19 08:57:19
  • 19

Common Subsequence POJ - 1458

Common Subsequence POJ - 1458 求两序列的最长公共序列。 模板 L[i][j]=L[i-1][j-1]+1;更新 L[i][j]=max(L[i-1][j],L[...
  • Miranda_ymz
  • Miranda_ymz
  • 2018-04-16 20:28:00
  • 4

HDU-1159-Common Subsequence

题目链接题目大意:给出两个字符串,求两个字符串的最长公共字串。 思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例。abcfbc abfcab。 点击...
  • sxc1414749109
  • sxc1414749109
  • 2015-05-26 20:39:22
  • 286

hdu1159Common Subsequence(动态规划之最长公共子序列)

Common Subsequence Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)...
  • zs520ct
  • zs520ct
  • 2016-04-27 10:30:40
  • 171

【HDU 1159】Common Subsequence(最长公共序列LCS算法)

Problem DescriptionA subsequence of a given sequence is the given sequence with some elements (possi...
  • iceiceicpc
  • iceiceicpc
  • 2016-07-26 09:26:30
  • 199

【LCS】POJ1458-Common Subsequence(模板)+LCS 的DP值算法解释

又到了本宝宝讲故事的时间辣!好吧并不是什么好故事。。。话说成天A题能找到老婆吗!没关系我老婆是电脑……诶。。。←这句话不是我说的       还是看题吧,模板题不记录一下会很不爽,这是初...
  • silent0001
  • silent0001
  • 2016-08-19 19:58:48
  • 201

POJ 1458-Common Subsequence (动态规划)

      题目大意:就是找两个字符串的最长公共子序列的长度。              例如: abcfbc  和 abfcab  的最长公共子序列为 abcb, 所以输出 4.  /*******...
  • morgan_xww
  • morgan_xww
  • 2010-05-16 19:16:00
  • 1546
    个人资料
    持之以恒
    等级:
    访问量: 15万+
    积分: 4849
    排名: 7376
    最新评论