Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39009   Accepted: 15713

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
最长公共子序列问题。
二维dp。dp[i][j]代表字符串s的前i个字符与字符串t的前j个字符的最长公共子序列的长度。dp[0][0]=0;
if(s[i]==t[j])dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);//从前状态取最大
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 510;
#define LL long long
int dp[maxn][maxn]={0};
char s[maxn],t[maxn];
int main()
{
while(scanf("%s %s",s,t)!=EOF)
{
int ls=strlen(s),lt=strlen(t);
for(int i=1;i<=ls;i++)
for(int j=1;j<=lt;j++)
dp[i][j]=s[i-1]==t[j-1]?dp[i-1][j-1]+1:max(dp[i-1][j],dp[i][j-1]);
printf("%d\n",dp[ls][lt]);
}
return 0;
}

POJ 1458-Common Subsequence(线性dp/LCS)的更多相关文章

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

    题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...

  2. poj 1458 Common Subsequence(dp)

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46630   Accepted: 19 ...

  3. poj 1458 Common Subsequence(区间dp)

    题目链接:http://poj.org/problem?id=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1 ...

  4. POJ 1458 Common Subsequence (zoj 1733 ) LCS

    POJ:http://poj.org/problem?id=1458 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=73 ...

  5. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

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

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  7. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

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

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

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

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

  10. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

随机推荐

  1. C# FTP操作

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...

  2. centos 6.5 服务器安装 (LNMP ntfs文件支持 PHP-RPM CHROOT沙盒)

    centos 6.5 最小化安装 进入系统 手动开启网卡 #ifconfig eth0        //(默认会自动获得ip) 修改网站配置文件,默认开启 #cd /etc/sysconfig/ne ...

  3. php设计模式之简单工厂模式

    ①抽象基类:类中定义抽象一些方法,用以在子类中实现 ②继承自抽象基类的子类:实现基类中的抽象方法 ③工厂类:用以实例化所有相对应的子类 /** * * 定义个抽象的类,让子类去继承实现它 * */ a ...

  4. MySql存储过程—3、变量

    1.变量的定义 在Mysql里面可以像我们写代码中一样定义变量来保持中间结果,看下面的格式: DECLARE variable_name datatype(size) DEFAULT default_ ...

  5. Unity3d在安卓android的更新(APK覆盖)

    其实这并没什么技术难点,也不是完美的热更新方案,只能说是退而求其次的一个方法. 起因主要是因为公司几个U3D项目在立项之初都没有能做好热更新的规化,导致现在要去做U3D的热更新非常难,并且项目已处于中 ...

  6. 测试Flask+PYTHON的WEB框架

    参数URL: http://blog.csdn.net/qwiwuqo/article/details/8970621 安装flask之前,你必须要先安装python和easy_install. 安装 ...

  7. Qt一步一步实现插件调用(附源码)

    最近手里几个项目都采用插件的方式进行开发工作,这里记录一下实现方法,给需要的同学一个参考, 在linux系统和window系统都能成功编译通过,不废话直接步骤 第一步:建立插件原型 新建一个Qt项目, ...

  8. Markdown和reStructuredText语法比较

    reStructuredText在线编辑器 http://rst.ninjs.org/ ReST是Docutils的标记语法,Docutils是Python世界的文档工具集.也因为这样ReST在Pyt ...

  9. windows下和linux下 Redis 安装

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  10. v$session_wait p1 p1raw p1_16

    SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...