Common Subsequence

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 51319   Accepted: 21146

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

 
 //2017-04-04
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
int dp[N][N];//LCS, dp[i][j]表示a[0~i]和b[0~j]的公共子序列长度 int main()
{
string a, b;
int n, m;
while(cin>>a>>b)
{
n = a.length();
m = b.length();
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+]);
}
}
cout<<dp[n][m]<<endl;
} return ;
}

POJ1458(KB12-L LCS)的更多相关文章

  1. DP总结 ——QPH

    常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...

  2. BZOJ2534 Uva10829L-gap字符串 字符串 SA ST表

    原文链接https://www.cnblogs.com/zhouzhendong/p/9240665.html 题目传送门 - BZOJ2534 题意 有一种形如 $uvu$ 形式的字符串,其中 $u ...

  3. BZOJ2119 股市的预测 字符串 SA ST表

    原文链接https://www.cnblogs.com/zhouzhendong/p/9069171.html 题目传送门 - BZOJ2119 题意 给定一个股票连续$n$个时间点的价位,问有多少段 ...

  4. 【NOI 2016】优秀的拆分

    Problem Description 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 \(A\) 和 \(B\) 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 ...

  5. [python] 获得所有的最长公共子序列

    两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...

  6. BZOJ 2119: 股市的预测 SA

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 434  Solved: 200[Submit][Status][Discuss ...

  7. JavaWeb 后端 <二> 之 Servlet 学习笔记

    一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...

  8. HTML5 3D 粒子波浪动画特效DEMO演示

    需要thress.js插件:     http://github.com/mrdoob/three.js // three.js - http://github.com/mrdoob/three.js ...

  9. 最长公共子序列LCS(POJ1458)

    转载自:https://www.cnblogs.com/huashanqingzhu/p/7423745.html 题目链接:http://poj.org/problem?id=1458 题目大意:给 ...

  10. HDU1159 && POJ1458:Common Subsequence(LCS)

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

随机推荐

  1. 项目中jsp的存放

    今天早上犯的错, 复习jsp,把jsp放在 WEB-INF 下  我启动项目,想看效果的时候,一直找不到指定的资源404 后来一番探索,我把它拖放在了WebContent  直接访问的,下午看到知识点 ...

  2. sublime text syntaxdef

    http://sublimetext.info/docs/en/extensibility/syntaxdefs.html

  3. 值不能为 null。 参数名: source

    今天调试程序总是报一个异常: 值不能为 null.参数名: source 异常详细信息: System.ArgumentNullException: 值不能为 null.参数名: source 通过断 ...

  4. Data - Hadoop伪分布式配置 - 使用Hadoop2.8.0和Ubuntu16.04

    系统版本 anliven@Ubuntu1604:~$ uname -a Linux Ubuntu1604 4.8.0-36-generic #36~16.04.1-Ubuntu SMP Sun Feb ...

  5. c++中的const和volatile知识自我总结

    学习了下c++中的const关键字,总结如下. 1.const限制一个变量不能修改其内容,如果强行修改的话,如下面代码这样子,编译就会报错,“表达式必须是可修改的左值”. int main() { c ...

  6. python _、__、__xx__之间的差别

    默认情况下,Python中的成员函数和成员变量都是公开的(public),在python中没有类public,private等关键词来修饰成员函数和成员变量.其实,Python并没有真正的私有化支持, ...

  7. 微信 OAuth2 网页授权获取用户信息

    文档:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html !!! 微信跟用户没有关系类接口采用了OAUTH2 [ ...

  8. sql字符处理

    --Description: 字符处理 --使用: 放到查询分析器里执行就可以了 --示例: select * from dbo.splitstr('12 44 45 50 56 87',' ') o ...

  9. Linux 启动过程详解

    目录 1. Linux启动过程 2. 启动过程概述 3. 引导加载阶段 4. 内核阶段 4.1 内核加载阶段 4.2 内核启动阶段 5. 早期的用户空间 6. 初始化过程 6.1 SysV init ...

  10. epoll 触发模式

    Edge Triggered (ET):边缘触发只有数据到来,才触发,不管缓存区中是否还有数据.Level Triggered (LT):水平触发只要有数据都会触发. LT(level trigger ...