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. Nintex Workflow Get Attachment link

    不多解释,直接上图,操作简单

  2. TLS/SSL

  3. Civil 3D 二次开发 创建Civil 3D 对象—— 00 ——

    本节中我们通过创建几何空间点.曲面和采样线了解Civil 3D对象的创建方法.因Civil 3D对象的创建方法相比AutoCAD对象创建要简单的多,比如创建一个几何空间点,最简单的情况采用一行代码(没 ...

  4. CODEFORCES掉RATING记 #1

    时间:2017.7.16晚 比赛:Educational Codeforces Round 25 比赛开始前去睡觉了...开始后5min才起来 一进去就点开AB,B先加载好,就先做了B.读完题后发现是 ...

  5. Mysql 语句优化

    通过 show status 命令了解各个 sql 语句的执行频率格式:Mysql> show [session | global] status;注:session 表示当前连接global ...

  6. Apache Ignite上的TensorFlow

    任何深度学习都是从数据开始的,这是关键点.没有数据,就无法训练模型,也无法评估模型质量,更无法做出预测,因此,数据源非常重要.在做研究.构建新的神经网络架构.以及做实验时,会习惯于使用最简单的本地数据 ...

  7. IT人员必须掌握的10项软技能

    现代企业的IT部门员工在具备技术能力的同时,还需要掌握一些软技能.现在来看这些软技能其实并不是什么新鲜事物,只是IT人员所需的这些软技能的范围与数量正在不断增加着. IT人员需要具备哪些技术能力,要取 ...

  8. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  9. hexo+next主题目录解析

    默认目录结构: . ├── .deploy ├── public ├── scaffolds ├── scripts ├── source | ├── _drafts | └── _posts ├── ...

  10. JavaWeb基础之Servlet简单实现用户登陆

    学习javaweb遇到了一些坑,一些问题总结下来,记个笔记. 学习servlet遇到的一些坑: servlet实现用户登陆遇到的坑解决办法: https://www.cnblogs.com/swxj/ ...