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. 一、关于a标签伪类中的visited不起作用问题

    一.代码示范 <html> <head> <title>伪类超链接</title> <!--<link href="./test. ...

  2. 跳转语句之continue

    js里面有两个跳转语句,一个是continue,一个是break.由于这两个跳转语句都是用于循环当中,因此他们也就只能用于while.for.do…while语句中,当然了,break多加一个swit ...

  3. 学习 Spring (八) 注解之 Bean 的定义及作用域

    Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...

  4. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  5. hdu 4578 Transformation 线段树多种操作裸题

    自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...

  6. codeforces498C

    Array and Operations CodeForces - 498C You have written on a piece of paper an array of n positive i ...

  7. codeforces362B

    Petya and Staircases CodeForces - 362B 题意: 一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个 ...

  8. 第四十天 并发编程之io模型

    一.今日内容 1.网络IO的两个阶段 waitdata copydata 2阻塞IO模型 之前写的都是阻塞 无论多线程 多进程 还是 进程池 线程池 3.非阻塞IO模型 在非阻塞IO中 需要不断循环询 ...

  9. 多校#5-1005-Instring-HDU5785-manacher+维护

    寻找三元组(i,j,k),使得(i,j)(j,k)都是回文字串,其中i<=j<k. 可以发现,对于每一位i,只要预处理出来有多少个以i为右端的回文串和以i为左端的回文串.把那些串的另一端的 ...

  10. 洛谷P4782 2-SAT问题

    2-SAT问题 这是一道2-SAT的模板题.对于2-SAT问题的每一个条件,我们需要把他们转化成可接受的条件.即"若变量A的赋值为x,则变量B的赋值为y",其中x,y均等于0或1. ...