Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 34477   Accepted: 13631

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

 
题意:
求两个字符串的最长公共子序列(lcs)的长度。
思路:
金典问题,动态转移方程如下:
 if(i==||j==)
dp[i][j]=;
else if(x[i]==y[i])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);

代码如下:

 #include<iostream>
#include<string>
#include<cmath>
using namespace std;
const int MAX=;
int dp[MAX][MAX]={};
int max(int a,int b)
{
return a>b?a:b;
}
int main(int *argv,int *argc[])
{
int len1,len2,i,j;
string str1,str2; //有时在时间允许范围内,用cin读字符串比较方便
while(cin>>str1>>str2)
{
len1=str1.length();
len2=str2.length();
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
//转移方程
{
if(str1[i-]==str2[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
cout<<dp[len1][len2]<<endl;
}
return ;
}

poj-------Common Subsequence(poj 1458)的更多相关文章

  1. (最长公共子序列 暴力) Common Subsequence (poj 1458)

    http://poj.org/problem?id=1458 Description A subsequence of a given sequence is the given sequence w ...

  2. HDU 1159 Common Subsequence(POJ 1458)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  3. Common Subsequence POJ - 1458 最长公共子序列 线性DP

    #include <iostream> #include <algorithm> #include <string> #include <cstring> ...

  4. LCS POJ 1458 Common Subsequence

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

  5. OpenJudge/Poj 1458 Common Subsequence

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

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

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

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

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

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

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

  9. Poj 1458 Common Subsequence(LCS)

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

  10. poj 1458 Common Subsequence(区间dp)

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

随机推荐

  1. 深入分析JavaWeb Item7 -- HttpServletResponse详解

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  2. 正则表达式和grep

    本章主要通过一些应用实例,来对正则表达式进行说明. 1.正则表达式 正则表达式就是字符串的表达式.它能通过具有意义的特殊符号表示一列或多列字符串.grep是linux系统下常用的正则表达式工具,可以使 ...

  3. 点云数据(point cloud) 【转】

    转自caimagic的专栏 一:什么是点云数据 点云数据是指在一个三维坐标系统中的一组向量的集合.这些向量通常以X,Y,Z三维坐标的形式表示,而且一般主要用来代表一个物体的外表面形状.不经如此,除(X ...

  4. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  5. Retrofit 从入门到了解【总结】

    源码:https://github.com/baiqiantao/RetrofitDemo.git 参考:http://www.jianshu.com/p/308f3c54abdd Retrofit入 ...

  6. Cognos第三方权限认证Oracle用户库

    一:概要描述 1.1:项目背景 Cognos具有强大的报表功能,但是却没有提供一个完善的用户管理体系,针对商业智能系统对数据的安全性要求,我们必须实现不同用户对不同数据的访问,确保企业级以及部门级的数 ...

  7. win10 右键发送到 目录

    win10 右键发送到 目录 C:\Users\llskj\AppData\Roaming\Microsoft\Windows\SendTo C:\Users\llskj\AppData\Roamin ...

  8. [Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)

    This post is similar to previous post. The difference is in this post, we are going to see how to ha ...

  9. 在简化版Fedora8上安装jdk-7u25-linux-i586.rpm的过程

    台式机的操作系统重新换回了Fedora8,遵从一些大牛的建议,把很多附件去了,尽量让系统保持最简化.这样能熟悉每个软件的安装配置过程,也能减少版本间的冲突. 进入控制台后,查查有没有Java存在系统中 ...

  10. 破解无线网络密码-BT3如何使用1

    一分钟制作 BT3 U盘版 方便,快捷简单 光盘版BT3, 大概694MB,直接刻盘,然后用光盘引导,即可进入bt3,连接为: http://ftp.heanet.ie/mirrors/backtra ...