Common Subsequence


Descriptions:

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, x ij = 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

题目连接:https://vjudge.net/problem/POJ-1458

题目大意

给出两个字符串,先求出这样的一个最长的公共子序列的长度:子序列中的每个字符串都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致

设数组dp[i][j],i表示第一个字符串的位置i,j表示第二个字符串的位置j,如果s1[i]==s2[j]j,那么dp[i][j]=dp[i-1][j-1]+1.否则dp[i][j]=max(dp[i-1][j],dp[i][j-1]).理解不了的,可以按样例一画一个表格试试就知道了

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define ME0(X) memset((X), 0, sizeof((X)))
using namespace std;
string s1,s2;
int dp[][];
int main()
{
while(cin >> s1 >> s2)
{
ME0(dp);
int len1=s1.length();
int len2=s2.length();
for(int i=; i<=len1; i++)
{
for(int j=; j<=len2; j++)
{
if(s1[i-]==s2[j-])
dp[i][j]=dp[i-][j-]+;
else
{
dp[i][j]=max(dp[i][j-],dp[i-][j]);
}
}
}
cout << dp[len1][len2] << endl;
}
}

【POJ - 1458】Common Subsequence(动态规划)的更多相关文章

  1. LCS POJ 1458 Common Subsequence

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

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

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

  3. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  4. OpenJudge/Poj 1458 Common Subsequence

    1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...

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

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

  6. Poj 1458 Common Subsequence(LCS)

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

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

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

  8. poj 1458 Common Subsequence

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

  9. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

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

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

随机推荐

  1. Java反射常用示例

    package xmq.study.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Const ...

  2. 关键字检索高亮标出-javasript/jQuery代码实现

    原文:http://www.open-open.com/code/view/1454504432089 此方法传入2个参数,一个是被检索内容所在的表单或者HTML元素的ID,另一为关键字,多个关键字的 ...

  3. nexus-3本地下载jar的settipng.xml配置

    打开maven安装目录下的setting.xml <servers> <server> <id>nexus</id> <username>a ...

  4. 【Todo】开个文章学VUE咯

    2017年FE架构组制定的框架选型主导为VUE.看了一下VUE的介绍,很不错. 开学~ https://www.zhihu.com/question/38213423 这个里面有VUE应用和背景的一些 ...

  5. 怎样扩展EasyUI在页面中马上显示选中的本地图片

    在编写前台页面的时候,有时须要将选中的图片夹杂着其它信息一起上传到服务端,在选着本地图片的时候,为了获得更好的效果,须要将该图片显示在页面上. 最初思路有两个.详细例如以下: 1.获取选中文件的二进制 ...

  6. Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)

    Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...

  7. Visual Studio VS如何拷贝一个项目的窗体文件到另一个项目

    1 比如下我有一个项目,我要把这个Config整个窗体和代码拷贝到另一个项目   2 在新项目中添加现有项,然后把这个窗体相关的三个文件都添加到新的项目中   3 然后在新窗体中就什么都有了     ...

  8. react-redux 之 provider 和 connect

    1.Provider 提供的是一个顶层容器的作用,实现store的上下文传递 2.connect 可以把state和dispatch绑定到react组件,使得组件可以访问到redux的数据 react ...

  9. mac下配置phonegap(cordova)5.1.1开发环境

    眼下最新的cordova的版本号是5.1.1,在mac下搭建开发环境过程例如以下: 1)首先安装NODEJS环境 进入官网: http://nodejs.org/ .眼下的版本号: v0.12.7 点 ...

  10. java中 ++前后差别试题及静态变量一旦赋值不可改变

    package javaTest; public class Increment { private static int k=0; public static void main(String[] ...