解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例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. JDK源码中的英文注释翻译(Enum<E extends Enum<E>>)

    public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...

  2. centos7 redmine安装过程(转载)

    redmine 部署过程 redmin官方文档写的太烂加上不熟悉ruby搞了半天,回到家后觉得还是记录下好,希望可以帮助有需要的人,少走弯路. 版本说明 下面的版本很重要redmine 版本 3.3 ...

  3. Map之HashMap的get与put流程,及hash冲突解决方式

    在java中HashMap作为一种Map的实现,在程序中我们经常会用到,在此记录下其中get与put的执行过程,以及其hash冲突的解决方式: HashMap在存储数据的时候是key-value的键值 ...

  4. 用Navicat Prenium12连接Oracle数据库(oracle11g版本)时报错ORA-28547:connection to server failed,probable Oracle Net admin error.解决办法

    上网一查原来是oci.dll版本不对.因为Navicat是通过Oracle客户端连接Oracle服务器的,Oracle的客户端分为两种,一种是标准版,一种是简洁版,即Oracle Install Cl ...

  5. CodeForces-546D Soldier and Number Game 筛法+动态规划

    题目链接:https://cn.vjudge.net/problem/CodeForces-546D 题意 抱歉,我给忘了,现在看题目又看不懂: P 思路 筛法+dp 话说这个函数应该是积性函数,然后 ...

  6. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) B】Shashlik Cooking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 翻转一次最多影响2k+1个地方. 如果n<=k+1 那么放在1的位置就ok.因为能覆盖1..k+1 如果n<=2k+1 ...

  7. @value 注解获取属性文件中的值

    一.属性文件 db.properties name=jack 二.配置文件 applicationContext.xml <!-- 加载配置文件,该节点只能存在一个,所以用 * ,加载所有属性文 ...

  8. 简述Web Service通讯技术的搭建流程

    Web Service 基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级 ...

  9. Android4.0设置界面改动总结(二)

    今年1月份的时候.有和大家分享给予Android4.0+系统设置的改动:Android4.0设置界面改动总结 时隔半年.回头看看那个时候的改动.事实上是有非常多问题的,比方说: ①.圆角Item会影响 ...

  10. [BZOJ 3884][欧拉定理]上帝与集合的正确使用方法

    看看我们机房某畸形写的题解:http://blog.csdn.net/sinat_27410769/article/details/46754209 此题为popoQQQ神犇所出,在此orz #inc ...