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

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 最长公共子序列问题(LCS) 其状态转换式为:A[i] = A[j]时,d(i,j) = d(i-1,j-1) + 1,否则d(i,j) = max{d(i-1,j),d(i,j-1)}
这个用char数组吧,用string可能出错,。。。打表
C++代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int dp[maxn][maxn];
char s1[maxn];
char s2[maxn];
int len1,len2;
int main(){
while(~scanf("%s%s",s1,s2)){
len1 = strlen(s1);
len2 = strlen(s2);
for(int i = ; i <= len1; i++){
dp[i][] = ;
}
for(int j = ; j <= len2; j++){
dp[][j] = ;
}
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]);
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}

(线性dp,LCS) 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 DP最长公共子序列(LCS)

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

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

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

  5. Poj 1458 Common Subsequence(LCS)

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

  6. poj 1458 Common Subsequence【LCS】

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

  7. poj 1458 Common Subsequence(dp)

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

  8. OpenJudge/Poj 1458 Common Subsequence

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

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

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

随机推荐

  1. 转 Pycharm及python安装详细教程

    转 : http://blog.csdn.net/qq_29883591/article/details/52664478 首先我们来安装Python 1.首先进入网站下载:点击打开链接(或自己输入网 ...

  2. jsp小测试--猜大小

    page1.jpg: <%@ page language="java" import="java.util.*" pageEncoding="g ...

  3. eclipse中git推送上传错误 没有足够的数据写入

    Can't connect to any repository: https://github.com/jiashubing/test.git (https://github.com/jiashubi ...

  4. thinkPHP框架5.0 类图下载

    thinkPHP5.0 类图下载

  5. 【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"

    问题定位:从描述就可以看的出来了,你现在sign_type是  RSA类型的,要改成跟你现在用的签名类型一致的类型,也就是 要改为 RSA2 PHP为例 // 新版只支持此种签名方式 商户生成签名字符 ...

  6. 每天一个Linux命令(03):du命令

    du命令 今天找开发定位问题,看到他使用了这个命令,查看文件,之前知道df,所以今天的每天系列把这命令 du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空 ...

  7. 「HAOI2018」染色 解题报告

    「HAOI2018」染色 是个套路题.. 考虑容斥 则恰好为\(k\)个颜色恰好为\(c\)次的贡献为 \[ \binom{m}{k}\sum_{i\ge k}(-1)^{i-k}\binom{m-k ...

  8. linux(fedora) 第一课

    1.Linux查看ip地址:ifconfig(interface config) 2.find / -name ifconfig (查找 从/开始找 找名字 匹配ifconfing) 复制命令:Ctr ...

  9. Win10 安装 Linux子系统 Ubuntu18.04 / Kali Linux 的体验

    汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#linux 几年前就看到新闻,今天周末,突发奇想,家里电脑安装下子系统不就不用安装开发的那些环 ...

  10. LOJ#2249 购票

    解:转移方程写出来,发现是斜率优化.因为在树上,考虑CDQ分治 + 点分治的方法... 每次找到重心,然后先递归解决上面的子树.然后把上面子树的凸包搞出来,下面每个点在凸包上二分找最优决策. 重心自己 ...