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. CodeForces 597A Divisibility

    水题. #include<iostream> #include<cstring> #include<cmath> #include<queue> #in ...

  2. Junit中Assert.assertEquals()和Assert.assertSame方法有什么异同

    1)提供的接口数量不完全相同.assertEquals支持boolean,long,int等等java primitiveType变量.assertSame只支持Object. 2)比较的逻辑不同,结 ...

  3. 使用mysql-connector-java.jar连接MySql时出现:Error while retrieving metadata for procedure columns: java.sql.SQLException: Parameter/Column name pattern can not be NULL or empty.

    错误如下: 程序实现的功能是调用一个存储过程,但是不认这个存储过程的参数. 原因是版本太高了,由于使用的是6.0.6版本的,改成5.1.38即可. POM配置如下: <!-- mysql-con ...

  4. hp 88a加粉

    http://v.youku.com/v_show/id_XNzEzODEwNzMy.html

  5. WinCE5.0如何安装.NET3.5

    首先去微软官网下载.NET Compact Framework 3.5 Redistributable 点击下载 下载页面 一共有两种安装方式,我们先介绍常规的安装方式 1.设备连接到电脑,然后双击下 ...

  6. HDU 5301 Buildings(2015多校第二场)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  7. Failed to load resource: the server responded with a status of 404 (Not Found)

    Failed to load resource: the server responded with a status of 404 (Not Found) 报错情况:图标加载失败 原因分析:路径错误 ...

  8. 用Visual Studio高版本号打开低版本号的project,转换时出现错误:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方法是: 在电脑里面搜索发现   C:\Program Files\Microsoft Visual Studio 10.0\VC\bin   C:\Windows\winsxs\x86_netf ...

  9. #include<> 和 #include""的区别

    #include< file >编译程序会先到标准函数库中找文件 #include”file” 编译程序会先从当前目录中找文件 参考原文 转: 在C程序中包含文件有以下两种方法: (1)用 ...

  10. 配置pydot环境

    第一次配置pydot环境,过程还是比較曲折,看来对这样的模式还不是非常熟悉.断断续续弄了两天弄好了.都是些小要求,小细节问题. 安装的顺序也非常重要: 1.安装python-2.7.8.amd64.m ...