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. 仿LOL项目开发第九天

    仿LOL项目开发第九天 by 草帽 OK,今天我们完全换了一种风格,抛弃了Unity3d的c#语法,我们来写写java的项目. 说到java服务器,当然有些人可能鄙视java的服务器速度太慢,但是相对 ...

  2. jsonConfig使用方法

    1.先编写jsonConfig的初始化代码 private JsonConfig jsonConfig; public action构造方法() { jsonConfig = new JsonConf ...

  3. [PHP] ubuntu16.04配置Lamp环境(搭建linux+apache+mysql+php7环境)

    reference : http://blog.csdn.net/Abyss_sliver/article/details/77621404 好久没有在Linux环境下进行开发了,比较常用的还是win ...

  4. 常用数据类型对应字节数,int长度

    常用数据类型对应字节数: 这两台机器,前者32位,后者64位,测试了以下数据类型的长度: 前者: ,, 后者: ,, 不是说int会变吗,为何变得是long? 还有如果要写个通用的程序,订死必须用4个 ...

  5. 数据库实例: STOREBOOK > 用户

    ylbtech-Oracle:数据库实例: STOREBOOK  >  用户 用户 1.返回顶部 1.1, 1.2, 2. 用户列表(用户状态=OPEN)返回顶部 2.1, DBSNMP 2.2 ...

  6. [18] 螺旋楼梯(Spiral Stairs)图形的生成算法

    顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...

  7. [转]VS2010快捷键

    VS2010的快捷键,后附VS2008的快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 ...

  8. jwplayer 隐藏属性方法记载

    jwplayer().getPosition(): //播放了多少秒 jwplayer('playerdiv').play(); || jwplayer(0).play(true / false); ...

  9. python中的exec()、eval()以及complie()

    参考博客:http://www.cnblogs.com/yyds/p/6276746.html 1.eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的python代码只能是单个表达式( ...

  10. MongoDB学习笔记(三)--权限 && 导出导入备份恢复 && fsync和锁

    权限                                                                                             绑定内网I ...