Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9595    Accepted Submission(s): 3923

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
 
 
 
  前面写了一份代码,一直不知到哪里处理错了,后来参考了别人的代码后发现了一个别人都是那样写,但是我却一直没注意的地方,那就是动态的方程在第一个元素的相等的时,dp[0][0] = dp[-1][-1] + 1, 天哪,这肯定就会出错了。在处理时可以选择字符的读取从第一个位置开始,或者把 i 号字符的状态存储到i+1号位置去,这样就从1号开始处理了,判定是就是 s1[i-1] == s1[j-1] ?
  代码如下:
 1 #include <cstring>

2 #include <cstdlib>

3 #include <cstdio>

4 #define Max( a, b ) (a) > (b) ? (a) : (b)

5 using namespace std;

6

7 char s1[1005], s2[1005];

8

9 int dp[1005][1005];

10

11 int main()

12 {

13 int len1, len2;

14 while( scanf( "%s %s", s1, s2 ) != EOF )

15 {

16 memset( dp, 0, sizeof(dp) );

17 len1 = strlen( s1 ), len2 = strlen( s2 );

18 for( int i = 1; i <= len1; ++i )

19 {

20 for( int j = 1; j <= len2; ++j )

21 {

22 if( s1[i-1] == s2[j-1] )

23 {

24 dp[i][j] = dp[i-1][j-1] + 1;

25 }

26 else

27 {

28 dp[i][j] = Max ( dp[i-1][j], dp[i][j-1] );

29 }

30 }

31 }

32 printf( "%d\n", dp[len1][len2] );

33 }

34 return 0;

35 }

  第二种处理方法:

#include <cstring>
#include <cstdlib>
#include <cstdio>
#define Max( a, b ) (a) > (b) ? (a) : (b)
using namespace std;

char s1[1005], s2[1005];

int dp[1005][1005];

int main()
{
    int len1, len2;
    while( scanf( "%s %s", s1+1, s2+1 ) != EOF )
    {
        memset( dp, 0, sizeof(dp) );
        len1 = strlen( s1+1 ), len2 = strlen( s2+1 );
        for( int i = 1; i <= len1; ++i )
        {
            for( int j = 1; j <= len2; ++j )
            {
                if( s1[i] == s2[j] )
                {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else
                {
                    dp[i][j] = Max ( dp[i-1][j], dp[i][j-1] );
                }
            }
        }
        printf( "%d\n", dp[len1][len2] );
    }

HDU-1159 Common Subsequence 最长上升子序列的更多相关文章

  1. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  2. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  3. HDU 1159 Common Subsequence

    HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...

  4. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  6. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  7. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  9. HDU 1159 Common Subsequence(裸LCS)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  10. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 03.C#委托(二章1.1)

    一章1.5-1.8介绍的是com.动态类型及.NET平台一些说明,每个心中都有自己的标准,听一家之言,叫人不爽,相信自己有自己的标准和自己的编程理念就OK了,也不想码那么多说明性的文字,直接跳过吧,当 ...

  2. Apache CXFjar包目录(转)

    文件目录结构及相关文件的详细说明:|-bin|-docs|-etc|-lib|-licenses|-modules|-samples bin(目录) bin 目录中是 CXF 框架中所提供的代码生成. ...

  3. javascript与服务器3

    一, 带参数的XMLHTTP请求 1, 进行get请求 get请求最常见的是在浏览器地址栏中输入URL并打开页面时,这就是向服务器发送一个get请求. 它的限制是URL最大长度不能超过2048字符(2 ...

  4. OC基础--关键字@property 和 @synthesize

    一.@property关键字需要掌握的知识: 1.用在@interface中,用来自动生成setter和getter的声明 例:@property int age;--相当于执行了右边的代码--> ...

  5. 小菜鸟 学MQ(二)

    mq服务启动以后 接着要做的事情就是 [发送]和[接受]消息. 首先有两种不同类型的Message:Topic,Queue 第一种Topic JMS规范定义了,Topic需要实现 发布和订阅两个功能, ...

  6. 利用css3实现超出文本指定行数与省略号效果

    <style> .text1 {/*单行*/ width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow: ...

  7. Cocos2d-X3.0 刨根问底(三)----- Director类源码分析

    上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...

  8. Opencv加载和显示图片

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...

  9. shell与变量的声明的操作

    1.给命令起别名:alias 执行下面命令后,可以使用dir代替ls –l 命令,显示目录中的文件详细信息: 还可以用一个别名表示几个命令 的结合: 2.ps:显示当前登录会话的所有活动进程: 3.更 ...

  10. MyEclipse------如何在特定目录下创建文件夹

    Directory.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...