Common Subsequence

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

Total Submission(s): 48378    Accepted Submission(s): 22242

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

LCS模板题,直接上代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
using namespace std;
char a[maxn],b[maxn];
int dp[maxn][maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>a>>b)
{
ms(dp);
int la=strlen(a);
int lb=strlen(b);
for(int i=1;i<=la;i++)
{
for(int j=1;j<=lb;j++)
{
if(a[i-1]==b[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[la][lb]<<endl;
}
return 0;
}

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 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

随机推荐

  1. 方便好用的 Idea mybatis 插件 MyBatisCodeHelper

    优点: 1.mapper文件(即表对应的dao)与xml文件自由切换,方便代码评审: 2.自动代码生成功能提高开发效率,mysql数据库创建好表结构,写完 pojo(注意字段类型要统一用对象类型!), ...

  2. Vue--项目开发之实现tabbar功能来学习单文件组件2

    上一篇文章里item.vue里的span标签内容是写死了,但是我们不希望写死 所以对于五个tab选项的标题需要从外部传入,也就说 需要在item.vue里的script里写上 export defau ...

  3. 在shell脚本里执行sudo 命令

      可以 : echo "yourpasswd" |sudo -S yourcommand 但是不安全,因为密码都显示在shell脚本里面了-_- 引自http://hi.baid ...

  4. 2-MAVEN 基本命令

    MVN的基本命令 mvn package:打包 >生成了target目录 >编译了代码 >使用junit测试并生成报告 >生成代码的jar文件 >运行jar包: java ...

  5. Asp.Net 中 HTTP 和 HTTPS 切换

    Asp.Net 中 HTTP 和 HTTPS 切换   目的 HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80 HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默 ...

  6. caffe的一些概念理解

    有一天,师姐问我,epoch和iteration有什么区别?我一时语塞,竟然遍寻百度而不得,最后在stackoverflow上找到一个我认为比较靠谱的答案,虽然它不是最高票,但是是最好理解的,深得我心 ...

  7. 每天CSS学习之text-overflow

    text-overflow是CSS3的一个属性,其作用是当文本溢出包含它的元素时,应该裁剪还是将多余的字符用省略号来表示. 该属性一般和overflow:hidden属性一起使用. text-over ...

  8. C++基础知识:泛型编程

    1.泛型编程的概念 ---不考虑具体数据类型的编程模式Swap 泛型写法中的 T 不是一个具体的数据类型,而是泛指任意的数据类型. 2.函数模板 - 函数模板其实是一个具有相同行为的函数家族,可用不同 ...

  9. 关于MEX函数的说明

    reference:http://www.mathworks.com/help/matlab/ref/mex.html .MEX文件是一种可在matlab环境中调用的C(或fortran)语言衍生程序 ...

  10. springboot学习章节代码-spring高级话题

    1.Spring Aware(获取Spring容器的服务) hi, i am guodaxia! test.txt package com.zhen.highlights_spring4.ch3.aw ...