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
 

题意:给两个字符串,求这两个字符串的最长公共子序列的长度。

序列特点:最长公共子序列中的字符出现的顺序和两个母串中出现的顺序是一样的。而最长公共子串则要求的更严格一点,子串中字符出现的顺序和母串中出现的顺序都必须是连续的;

代码如下:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#define N 1010
using namespace std; int dp[1010][1010];
int main ()
{
int n,m,i,j;
int len1,len2;
char a[N],b[N];
a[0]='-';
b[0]='-';
while(~scanf("%s %s",a+1,b+1)){//使得存的字符串从字符串的地二位开始存;
len1=strlen(a);
len2=strlen(b);
for(i=1;i<max(len1,len2);i++){//初始化
dp[i][0]=dp[0][i]=0;
}
for(i=1;i<len1;i++){
for(j=1;j<len2;j++){
if(a[i]==b[j]){
dp[i][j]=dp[i-1][j-1]+1;
}else {
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
/*for(i=0;i<len1;i++){
for(j=0;j<len2;j++){
printf("%d ",dp[i][j]);
}
printf("\n");
}*/
printf("%d\n",dp[len1-1][len2-1]);
}
return 0;
}

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. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

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

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

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

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

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

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

  8. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  9. LCS(Longest Common Subsequence)最长公共子序列

    最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...

  10. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

随机推荐

  1. rvm 安装ruby环境报错curl: (35) error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure

    很可能是rvm仓库版本过低,运行以下命令: rvm get head

  2. ASP.NET MVC之Bundle压缩JS和CSS

    介绍Bundle之前先引用<淘宝技术这十年>中一段话,对Web前端稍微有点常识的人都应该知道,浏览器下一步会加载页面中用到的CSS.JS(JavaScript).图片等样式.脚本和资源文件 ...

  3. NOI 16 买房子

    买房子(NOI 16) 总时间限制: 1000ms 内存限制: 65536kB 描述 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,假设房子价格以每年百分之K ...

  4. python 判断字符串是否以数字结尾

    import re def end_num(string): #以一个数字结尾字符串 text = re.compile(r".*[0-9]$") if text.match(st ...

  5. shell模拟ctrl c停止

    kill命令可以带信号号码选项,也可以不带. 如果没有信号号码,kill命令就会发出终止信号(15),这个信号可以被进程捕获,使得进程在退出之前可以清理并释放资源. 也可以用kill向进程发送特定的信 ...

  6. django字段的参数

    所有的模型字段都可以接收一定数量的参数,比如CharField至少需要一个max_length参数.下面的这些参数是所有字段都可以使用的,并且是可选的. null 该值为True时,Django在数据 ...

  7. R-FCN论文理解

    一.R-FCN初探 1. R-FCN贡献 提出Position-sensitive score maps来解决目标检测的位置敏感性问题: 区域为基础的,全卷积网络的二阶段目标检测框架: 比Faster ...

  8. 从celery rabbitmq with docker-compose 引出对容器、依赖注入、TDD的感悟

    用docker配置项目管理系统taiga的时候,不是我一个人遇到这个问题.https://github.com/douglasmiranda/docker-taiga/issues/5 问题描述: 用 ...

  9. Codeforces 535C - Tavas and Karafs

    535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...

  10. url 中需要转义的字符

    1. +  URL 中+号表示空格 %2B 2. 空格 URL中的空格可以用+号或者编码 %20 3. /  分隔目录和子目录 %2F  4. ?  分隔实际的 URL 和参数 %3F  5. % 指 ...