Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37614   Accepted: 15058

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

NYOJ同题

#include <stdio.h>
#include <string.h>
#define maxn 1000 char str1[maxn], str2[maxn];
int dp[maxn][maxn]; int max(int a, int b){ return a > b ? a : b; } int LCS()
{
int ans = 0;
for(int i = 1, j; str1[i]; ++i){
for(j = 1; str2[j]; ++j){
if(str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
}else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(dp[i][j] > ans) ans = dp[i][j];
}
}
return ans;
} int main()
{
while(scanf("%s%s", str1 + 1, str2 + 1) == 2){
printf("%d\n", LCS());
}
return 0;
}

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

  1. poj1458 Common Subsequence ——最长公共子序列

    link:http://poj.org/problem?id=1458 最基础的那种 #include <iostream> #include <cstdio> #includ ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. java的提取与替换操作

    public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班  2007032 ...

  2. 代码重构 & 代码中的坏味道

    1.重构 1.1 为什么要重构 1.1.1 改进程序设计 程序员为了快速完成任务,在没有完全理解整体架构之前就开始写代码, 导致程序逐渐失去自己的结构.重构则帮助重新组织代码,重新清晰的体现 程序结构 ...

  3. Mysql经常使用命令

    1.导出整个数据库 mysqldump -u username -p --default-character-set=latin1 数据库名 > 导出的文件名称(数据库默认编码是latin1) ...

  4. Git管理工具对照(GitBash、EGit、SourceTree)

    Git管理工具对照(GitBash.EGit.SourceTree) GitBash是採用命令行的方式对版本号进行管理,功能最为灵活强大,可是由于须要手动输入希望改动的文件名称,所以相对繁琐. EGi ...

  5. 大约 Apple Metal API 一些想法

    看后 Metal 的开发文档后,除了官方所宣称的一些长处外(比方说更easy理解和使用的 API.更直接和精细的硬件控制,降低 GPU 使用过程中的 CPU 额外开销等等),从我有限的 GLES 开发 ...

  6. Revit 2015 公布!

    Revit 2015 公布了, 如今能够下载.大家能够搜索下中文版的下载. 之前就知道2015 的模型操作速度再次提高, 2015安装后的马上载入跑了一个模型.果然,2015 打开自带的高级模型,不管 ...

  7. 《深入浅出 Java Concurrency》—锁紧机构(一)Lock与ReentrantLock

    转会:http://www.blogjava.net/xylz/archive/2010/07/05/325274.html 前面的章节主要谈谈原子操作,至于与原子操作一些相关的问题或者说陷阱就放到最 ...

  8. POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

    题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...

  9. Android他们控制的定义(一)

    培养自己的控制步骤定义: 1.要理解View作品  2. 分享到继承View子类 3. 要定义自己的View类添加属性  4. 绘制控件  5. 响应用户消息  6 .自己定义回调函数  一.View ...

  10. CentOS下tmux安装与使用

    tmux介绍: tmux它是BSDScreen替代品,相对于Screen,它更加先进:支持屏幕切分,并且具备丰富的命令行參数,使其能够灵活.动态的进行各种布局和操作.它能够做到一条命令就启动起来(强大 ...