题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度。

析:很明显是个DP,就是LCS,一点都没变。设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串LCS长度。

当A[i] = B[j] 时,这个最长度就是上一个长度加1,即:d(i, j) = d(i-1, j-1) + 1;

当A[i] != B[j] 时,那就是前面的最长长度(因为即使后面的不成立,也不会影响前面的),即:d(i, j) = max{d(i-1, j), d(i, j-1)}。

时间复杂度为mn,其中m,n分别为两个序列的长度。

代码如下:

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio> using namespace std;
const int maxn = 1000 + 10;
char s1[maxn], s2[maxn];
int d[maxn][maxn]; int main(){
while(~scanf("%s", s1+1)){
scanf("%s", s2+1);
int len1 = strlen(s1+1);
int len2 = strlen(s2+1); memset(d, 0, sizeof(d));
for(int i = 1; i <= len1; ++i)
for(int j = 1; j <= len2; ++j)
if(s1[i] == s2[j]) d[i][j] = d[i-1][j-1] + 1;
else d[i][j] = max(d[i-1][j], d[i][j-1]); printf("%d\n", d[len1][len2]);
}
return 0;
}

POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)的更多相关文章

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

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  2. poj 1458 Common Subsequence ——(LCS)

    虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #inc ...

  3. HDU 1159 Common Subsequence (动规+最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

  5. poj 1458 Common Subsequence【LCS】

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

  6. POJ 1458 Common Subsequence DP

    http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长 ...

  7. [dp]LCS最长公共子序列

    https://www.51nod.com/tutorial/course.html#!courseId=4 复杂度:${\rm O}(nm)$ 转移方程: #include<bits/stdc ...

  8. LCS(最长公共子序列问题)

    LCS(Longest Common Subsequence),即最长公共子序列.一个序列,如果是两个或多个已知序列的子序列,且是所有子序列中最长的,则为最长公共子序列. 原理:    事实上,最长公 ...

  9. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  10. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

随机推荐

  1. HttpClient post封装

    /** * @title HttpUtils * @description post请求封装 * @author maohuidong * @date 2017-12-18 */ public sta ...

  2. Haskell语言学习笔记(60)Biapplicative

    Biapplicative class Bifunctor p => Biapplicative p where bipure :: a -> b -> p a b (<< ...

  3. 【336】Tutorial of Endnote

    Now I start to use Endnote to manage my literatures and I need to learn how to use it. Below is some ...

  4. ora-01652无法通过128(在temp表空间中)扩展temp段

    有两种错误:1.数据表空间不足 2.临时表空间不足 有两种原因:一是临时表空间空间太小,二是不能自动扩展. 分析过程:    既然是temp表空间有问题,那当然就要从temp表空间说起啦.首先要说明的 ...

  5. Autopilot Pattern Redis

    https://github.com/autopilotpattern/redis Architecture 正在运行的群集包括以下组件: Redis :我们使用的是Redis 3.2. Redis ...

  6. At least one JAR was scanned for TLDs yet contained no TLDs.

    Tomcat提示如下: At least one JAR was scanned for TLDs yet contained no TLDs. =========================== ...

  7. Factorial Trailing Zeroes (Divide-and-Conquer)

    QUESTION Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should ...

  8. app电池续航上&&下--Android自动化测试学习历程

    章节:自动化基础篇——电池续航自动化测试上&&下 主要讲解内容及笔记: 一.影响手机电量的因素和理论: 下面是一个表格:当今主流手机显示屏技术.机型与功耗对照表:   技术  类型   ...

  9. 指针c艹

    #include <iostream> using namespace std;int value=1;void func(int *p){ p=&value; }void fun ...

  10. jquery源码学习-初始(1)

    最近几天一直在研究jquery源码,由于水平太低看得昏头转向.本来理解的也不是很深刻,下面就用自己的想法来说下jquery是如何定义构造函数初始化的.如果有什么不对的地方,希望个位高手指出. 首先要了 ...