链接:



Common Subsequence

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

Total Submission(s): 17621    Accepted Submission(s): 7401

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
 
Recommend
Ignatius
 



题意:


     求最长公共上升子序列
        就是第一个序列和第二个序列中有几个相同的。
        子序列:
                如果有一个序列 <A1, A2, A3,... An> 还有个序列 <Ak1, A k2, ... A kn> 满足 k1 < k2 < k3,
                那么 第二个序列是第一个序列的子序列
        最长公共子序列:
                就是从几个序列【一般是两个了】中找出一样的最长的子序列

分析:

用 dp[i][j] 记录序列 A 中从 0 到 i-1  和序列 B 中从 0  到 j-1  的最长公共子序列长度

O(n^n)写法:

先初始化 dp 为 0

当 A 序列遍历到第 i-1 个,序列 B  遍历到第 j-1 个

1) 如果此时 A[i-1] == B[j-1] , 那么可想而知 dp[i][j] = dp[i-1][j-1] +1
2) 如果此时 A[i-1] != B[j-1], 那么 dp[i][j] = max( dp[i-1][j],  dp[i][j-1] )

总之相当于记忆化搜索的了 dp[i][j] 从左到右从上到下, 当确立了当前字符是否相等时
那么 dp[i][j] 就由它的左上角 ( dp[i-1][j-1] )、上面的 ( dp[i-1][j] )、前面的 ( dp[i][j-1] ) 确定

/**
求长度为 len1 的序列 A 和长度为 len2 的序列 B 的LCS
注意:序列下标从 0 开始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
}


比完赛了学妹提到的,将 dp[maxn][maxn]优化到 dp[2][maxn]
其实也是滚动数组的概念了:
由上面的分析我们发现这一点:当前的 dp[i][j] 只是与以它为右下角的四个 dp 有关

dp[i-1][j-1]  dp[i-1][j]
dp[i][j-1]     dp[i][j]

而我们并不是要求出每一段的 LCS 而只是求出最终的长度的 LCS,那么每次对 i %2 就可以解决问题

这样就大大优化了内存
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
}

code:

普通:
A Accepted 1244 KB 31 ms C++ 749 B

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 500+10;
int dp[maxn][maxn];
char s1[maxn], s2[maxn]; /**
求长度为 len1 的序列 A 和长度为 len2 的序列 B 的LCS
注意:序列下标从 0 开始
*/
void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
else
{
int m1 = dp[i-1][j];
int m2 = dp[i][j-1];
dp[i][j] = max(m1, m2);
}
}
}
} int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp)); LCS(len1, len2);
printf("%d\n", dp[len1][len2]);
}
}

滚动数组:
Accepted 1159 31MS 232K 794 B C++

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 500+10;
int dp[2][maxn];
char s1[maxn], s2[maxn]; void LCS(int len1,int len2)
{
for(int i = 1; i <= len1; i++)
{
for(int j = 1; j <= len2; j++)
{
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1]+1;
else
{
int m1 = dp[(i-1)%2][j];
int m2 = dp[i%2][j-1];
dp[i%2][j] = max(m1, m2);
}
}
}
} int main()
{
while(scanf("%s%s", s1,s2) != EOF)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(dp,0,sizeof(dp)); LCS(len1, len2);
printf("%d\n", dp[len1%2][len2]);
}
}

hdu 1159 Common Subsequence 【LCS 基础入门】的更多相关文章

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

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

  2. HDU 1159 Common Subsequence (LCS)

    题意:给定两行字符串,求最长公共子序列. 析:dp[i][j] 表示第一串以 i 个结尾和第二个串以 j 个结尾,最长公共子序列,剩下的就简单了. 代码如下: #pragma comment(link ...

  3. HDU 1159 Common Subsequence

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

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

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

  5. HDU 1159 Common Subsequence(裸LCS)

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

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

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

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

    题目链接: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 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

随机推荐

  1. Netty4具体解释三:Netty架构设计

         读完这一章,我们基本上能够了解到Netty全部重要的组件,对Netty有一个全面的认识.这对下一步深入学习Netty是十分重要的,而学完这一章.我们事实上已经能够用Netty解决一些常规的问 ...

  2. 本体论与OWL

    http://semanticweb.org/wiki/Main_Page.html http://owl.man.ac.uk/documentation.shtml  https://zh.wiki ...

  3. AngularJS体验式编程系列文章

    AngularJS体验式编程系列文章,将介绍如何用angularjs构建一个强大的web前端系统.angularjs是由Google团队开发的一款非常优秀web前端框架.在当前如此多的web框架下,a ...

  4. Spring Boot(三):logback打印日志

    springboot对logback的支持是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以实现功能直接贴代码: <?xml version="1.0 ...

  5. MEF教程

    http://www.cnblogs.com/content/archive/2013/05/31/3111156.html

  6. python-list.sort && lambda

    dictionary是一个有元组组成的list,变量名有点歧义,这里是想表达 由元组组成的list哈. 之所以用dictionary是因为模拟的将字典转换成list. 将list进行排序,而根据lam ...

  7. Python内置函数之int()

    class int(x, base=10) 返回一个整型对象.默认返回0. 参数x可以是字符串,也可以是浮点数. base指x的进制形式,比如2表示2进制,10表示10进制.特别需要注意的是,0表示任 ...

  8. java中的锁池和等待池

    在java中,每个对象都有两个池,锁(monitor)池和等待池 wait() ,notifyAll(),notify() 三个方法都是Object类中的方法. 锁池:假设线程A已经拥有了某个对象(注 ...

  9. Oracle之配置客户端登陆多个远程数据库

    一.引言 一直搞不明白Oracle数据库的客户端是怎么回事,怎么配置,前几天由于工作中需要用到Oracle,而且需要连接两个不同的数据库,就通过上网和请教同事终于把客户端的配置搞定了,记录之,学习之 ...

  10. 我的vimrc文件

    vim的一直被称为神器,确实有很多优点,但是vim到一键编译实在是一个大问题,网络上有很多配置文件,但是大多都是同一份文件到复制粘贴,不太好用. 经过这么长时间到摸索,我终于在自己到电脑上配置好了vi ...