解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序列的长度

a    b    f    c    a    b

0    0    0    0    0   0    0

a  0    1     1    1    1   1    1

b  0    1     2    2    2   2    2

c  0    1     2    2    3   3    3

f  0     1     2    3    3   3   3

b 0     1    2     3   3    3   4

c  0     1    2     3   4   4    4

其中 s1 abcfbc

s2 abfcab

以s1中的a来分析,用它与s2中的字母比较,如果相同,那么dp[i][j]=dp[i-1][j-1]+1(遇到相同的字母公共子序列的长度增加1)

如果不同的话,dp[i][j]=max(dp[i-1][j],dp[i][j-1])                 (即取相邻两种情况中的最大值)

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39737   Accepted: 15977

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
#include<stdio.h>
#include<string.h>
#define maxn 10010
int dp[maxn][maxn]; int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
char s1[maxn],s2[maxn];
int len1,len2,i,j;
while(scanf("%s %s",&s1,&s2)!=EOF)
{
len1=strlen(s1);
len2=strlen(s2); for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
if(s1[i-1]==s2[j-1])
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]);
} }

  

POJ 1458 Common Subsequence 【最长公共子序列】的更多相关文章

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

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

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

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

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

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...

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

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

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

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

  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 最长公共子序列

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

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

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

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

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

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

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

随机推荐

  1. mach-o格式分析

    0x00 摘要 人生无根蒂,飘如陌上尘. 分散逐风转,此已非常身. — 陶渊明 <杂诗> mach-o格式是OS X系统上的可执行文件格式,类似于windows的PE与linux的ELF, ...

  2. 文件类型总结 MIME

    来源网上https://www.cnblogs.com/zhongcj/archive/2008/11/03/1325293.html {".3gp", "video/3 ...

  3. 历年真题 未完成(Noip 2008 - Noip 2017)

    Noip 2008 :全部 Noip 2009 :全部 Noip 2010 :AK Noip 2011 :AK Noip 2012 : Vigenère 密码,国王游戏,开车旅行 Noip 2013 ...

  4. Centos 7.2 安装和卸载 MySQL 5.7

    一.背景 闲暇之余在虚拟机安装了 Centos 7.2 系统,按照 <简单安装MySQL(RPM方式)> 这篇文章安装 MySQL ,发现由于包依赖的问题安装失败,于是索性在官网查询相关文 ...

  5. Django之组合查询

    一 .Django组合查询 很多电商网站中有组合搜索的功能,所谓组合搜索就是网页中组合多个条件,对数据库中进行查询,并且将结果显示在页面中,下面是所做组合查询效果图: 实现代码如下: <1> ...

  6. 【JavaScript框架封装】实现一个类似于JQuery的选择框架的封装

    // 选择框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法) xframe.extend({}); // 不需要参与链式访问的 ...

  7. [luogu2047 NOI2007] 社交网络 (floyed最短路)

    传送门 输入输出样例 输入样例#1: 4 4 1 2 1 2 3 1 3 4 1 4 1 1 输出样例#1: 1.000 1.000 1.000 1.000 题解 在进行floyed的过程中,顺便更新 ...

  8. 2019-04-03 Anaconda+VSCode搭建python开发环境,并连接GIthub

    1.最好的Python开发环境 :Anaconda+VSCode搭建python开发环境,conda提供了python开发环境和大量的你不用安装的库 conda的环境变量: 直接在conda 中下载启 ...

  9. 2019-03-28 git github SSH配置,上传下载操作

    1.通过git获取scrapy源码,并安装到系统里面 https://git-scm.com/download/win 下载无脑安装啊(C:\Program Files\Git),进入git bash ...

  10. java8新特性:利用Lambda处理List集合

    Java 8新增的Lambda表达式,我们可以用简洁高效的代码来处理List. 1.遍历 public static void main(String[] args) { List<User&g ...