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. APP的上线和推广——线上推广渠道

    本文版权归个人所有,如需转载请注明出处http://www.cnblogs.com/PengLee/p/4637080.html 目录 应用商店 互联网开放平台 软件下载中心 媒体社交平台 刷榜推广 ...

  2. Vim global命令和重复操作

    Vim global命令和重复操作 Vim global命令允许我们在某个指定模式的所有匹配行上运行可执行的 Ex 命令,缩写形式为 :g,其处理重复工作的效率极高. 一.Vim global命令介绍 ...

  3. C语言:返回两个数组中第一个元素的指针,并输出这个值

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年. All righ ...

  4. hdu 1242 Rescue(BFS入门)

    第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...

  5. Reorder List leetcode java

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must d ...

  6. STL sort 函数实现详解 ZZ

    前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不对劲,知道自己回答错了.这几天特意看了一下,在 ...

  7. PHPnow For ASP&&ASP.NET&&MongoDB&&MySQL支持VC6.0编译器&&MySQL升级

    可能和大家熟悉的是LAMP,Linux+Apache+Mysql+PHP,在Windows上,可能大家比较熟悉的是WAMP,Windows+Apache+Mysql+PHP,这是一个集成环境,说到集成 ...

  8. FM同步数据库中结构已经发生变化的表

    接触Cognos很久了,最近遇到一个小问题. 在FM模型设计的过程中,有一张表jd_f_order.之后为了更全面的分析这个数据,在这个事实表中引入了一个新的字段商品类型字段,结构如图 但是由于jd_ ...

  9. 一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码

    遍历表格的JS容易忘,留个随笔以备忘. var tableData="";    var table=document.getElementById("XXTableId ...

  10. C#.NET常见问题(FAQ)-delegate委托链如何使用

    委托链本质就是你把一堆要执行的东西放到一个list里面,当要触发一组事情的时候,就不需要一个一个写一遍了(比如厂里食堂开饭了,这个方法一执行,要让厨师A时间在食堂等候打饭,B类员工在某个时间排队打饭, ...