题目链接:http://poj.org/problem?id=1458

Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 55099   Accepted: 22973

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

Source

 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; char a[MAXN], b[MAXN];
int dp[MAXN][MAXN]; int main()
{
while(scanf("%s%s", a+, b+)!=EOF)
{
int n = strlen(a+);
int m = strlen(b+); ms(dp, );
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(a[i]==b[j])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = max(dp[i][j-], dp[i-][j]);
}
printf("%d\n", dp[n][m]);
}
}

POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)的更多相关文章

  1. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

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

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  3. POJ1458 Common Subsequence 【最长公共子序列】

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

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

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

  5. hdu 1159 Common Subsequence (最长公共子序列 +代码)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  6. POJ 1458 Common Subsequence 【最长公共子序列】

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

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

    题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...

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

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

  9. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

随机推荐

  1. MySQL索引与Index Condition Pushdown(二)

    实验 先从一个简单的实验开始直观认识ICP的作用. 安装数据库 首先需要安装一个支持ICP的MariaDB或MySQL数据库.我使用的是MariaDB 5.5.34,如果是使用MySQL则需要5.6版 ...

  2. 一张图表,人人都能建立自己的AARRR运营模型

    每次跟同行聊运营,聊用户,聊产品,最后都会回到AARRR模型上来,这个用户全生命周期模型概括了互联网产品运营的5个关键环节. 获客是运营的基础,促进用户活跃才能让产品有生命力,提升留存减少流失让用户规 ...

  3. 收集Windows 8 Metro UI 风格网站资源,觉得不错的顶啊!!

    这些资源包含:模板,框架,jQuery插件,图标集等.帮助你快速开发Windows 8 Metro UI风格的网站.本文转自虾米站长网 Frameworks & Templates For M ...

  4. 简单的发红包的PHP算法

    假设有有10元钱 ,发给10个人.保证每个人都有钱拿,最少分得0.01.我们最先想到的肯定就是随机.0.01-10随机.但是会出现第一个人就分得9.99的情况.下面就没人可分了.然后就是我的错误思路 ...

  5. JS Enter键跳转 控件获得焦点

    //回车跳转 jQuery(document).ready(function () { //$(':input:text:first').focus(); jQuery(':input:enabled ...

  6. 使用dd命令快速生成大文件或者小文件的方法

    使用dd命令快速生成大文件或者小文件的方法     转载请说明出处:http://blog.csdn.net/cywosp/article/details/9674757     在程序的测试中有些场 ...

  7. hdu 5200 Trees [ 排序 离线 2指针 ]

    传送门 Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 655 ...

  8. java类中资源加载顺序

    根据优先级别从高到低依次为:1.父类中的静态代码块(static);2.自身的静态代码块;3.父类中的的普通代码块;4.父类的构造方法;5.自身的普通代码块;6.自身的构造方法; 下面是一个测试 结果 ...

  9. cef3的各个接口你知道几个

    CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...

  10. eclipse环境下无法创建android virtual Devices(AVD)问题解决的方法汇总

    首先,要在eclipse环境下成功的创建一个安卓虚拟机,须要有三项东西,第一就是eclipse,第二就是android SDK Manager,第三就是ADT,也就是eclipse环境下的一个安卓虚拟 ...