Common Subsequence


Descriptions:

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, x ij = 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

题目连接:https://vjudge.net/problem/POJ-1458

题目大意

给出两个字符串,先求出这样的一个最长的公共子序列的长度:子序列中的每个字符串都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致

设数组dp[i][j],i表示第一个字符串的位置i,j表示第二个字符串的位置j,如果s1[i]==s2[j]j,那么dp[i][j]=dp[i-1][j-1]+1.否则dp[i][j]=max(dp[i-1][j],dp[i][j-1]).理解不了的,可以按样例一画一个表格试试就知道了

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define ME0(X) memset((X), 0, sizeof((X)))
using namespace std;
string s1,s2;
int dp[][];
int main()
{
while(cin >> s1 >> s2)
{
ME0(dp);
int len1=s1.length();
int len2=s2.length();
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]);
}
}
}
cout << dp[len1][len2] << endl;
}
}

【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 (动态规划)

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

  4. OpenJudge/Poj 1458 Common Subsequence

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

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

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

  6. Poj 1458 Common Subsequence(LCS)

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

  7. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

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

  8. poj 1458 Common Subsequence

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

  9. poj 1458 Common Subsequence【LCS】

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

  10. (线性dp,LCS) POJ 1458 Common Subsequence

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

随机推荐

  1. jree-创建普通折线图

    对于maven工程,需要引入依赖:在pom.xml中,添加如下内容 <dependency> <groupId>jfree</groupId> <artifa ...

  2. java的反射机制和javassist、asm

    1.java的反射机制,可以帮助我们在运行的时候获取我们引用的java类相关的信息,包括类的名字.所包含的方法名字.方法参数等等 2.javassit这个jar包,大概看了下,更厉害,它可以直接操作字 ...

  3. Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>

    这是因为架包没导对或者关联的项目不是在同一个工作空间.

  4. CSDN管理员看过来

    CSDN管理员看过来 你好.CSDN管理员,我想我被特殊对待了.我看了一些人的博客.终于发现仅仅有我博客的数据有异常.这算是给我的惊喜吗? 言归正传,我发现我博客上两个地方出现的文章的总数对不上.原创 ...

  5. Web安全漏洞及攻击

    背景介绍 先说一个在互联网上常见,但是普通人又不太理解的东西--“验证码”. 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell ...

  6. 使用python在极坐标中生成一条直线

    在测试雷达时,往往需要测试雷达的数据是否准确,这时就需要在雷达图中显示一条标准的直线作为对比. "create a wall" import numpy as np import ...

  7. iOS学习之动画效果的实现

    // //  ViewController.m //  UI-动画练习 // //  Created by jzq_mac on 15/7/22. //  Copyright (c) 2015年 jz ...

  8. linux系统编程:线程同步-相互排斥量(mutex)

    线程同步-相互排斥量(mutex) 线程同步 多个线程同一时候訪问共享数据时可能会冲突,于是须要实现线程同步. 一个线程冲突的演示样例 #include <stdio.h> #includ ...

  9. 嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析

     #define SBULL_MINORS  16         /* 每个sbull设备所支持的次设备号的数量 */  #define KERNEL_SECTOR_SIZE 512  // 本地定 ...

  10. createElement与innerHtml性能比較

    js中.动态加入html的方法大致就是两种,一种是document.createElement等方法创建,然后使用Element.appendChild加入,或者是使用Element.innerHTM ...