class Solution:
def LCS(self,A,B):
if not A or not B: #边界处理
return 0
dp = [[0 for _ in range(len(B)+1)]for _ in range(len(A)+1)]#状态定义,dp[i][j]表示当前最长公共的长度
for i in range(1,len(A)+1): #遍历A序列
for j in range(1,len(B)+1): #遍历B序列,时间复杂度为n2
if A[i-1] == B[j-1]: #如果此时两个值相等
dp[i][j] = dp[i-1][j-1] + 1 #状态转移为前一时刻状态加1
else: #不相等的话
dp[i][j] = max(dp[i][j-1],dp[i-1][j]) #当前时刻的状态为,前两个时刻的较大值
print(B)
self.printDP(dp)
return dp[-1][-1] #最优解是最后的状态值
def printDP(self,dp): #打印转态
for i in range(len(dp)):
for j in range(len(dp[i])):
print(dp[i][j],end=' ')
print() if __name__ == '__main__':
solution = Solution()
A = [1, 2, 3, 5, 2, 1]
B = [3, 2, 1, 4, 7]
res = solution.LCS(A,B)
print('最长公共子序列长度为:',res)

结果如下:[3,2,1]是最长的共子序列

最长公共子序列 DP的更多相关文章

  1. LCS最长公共子序列~dp学习~4

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...

  2. POJ 1458 最长公共子序列(dp)

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

  3. 【BZOJ2423】[HAOI2010]最长公共子序列 DP

    [BZOJ2423][HAOI2010]最长公共子序列 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

  4. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  5. 38-最长公共子序列(dp)

    最长公共子序列 https://www.nowcoder.com/practice/c996bbb77dd447d681ec6907ccfb488a?tpId=49&&tqId=293 ...

  6. bzoj3304[Shoi2005]带限制的最长公共子序列 DP

    题意:给出三个序列,求出前两个的公共子序列,且包含第三个序列,要求长度最长. 这道题目怎么做呢,f[i][j]表示a串1-i,b串1-j的最长,g[i][j]表示a串i-n,b串j-m最长, 那么只需 ...

  7. 洛谷-P1439 【模板】最长公共子序列 (DP,离散化)

    题意:给两个长度为\(n\)的全排列,求他们的LCS 题解:这题给的数据范围到\(10^5\),用\(O(n^2)\)的LCS模板过不了,但由于给的是两个全排列,他们所含的元素都是一样的,所以,我们以 ...

  8. 题目1042:Coincidence(最长公共子序列 dp题目)

    题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. 求最长公共子序列-DP问题

    Longest common subsequence problem The longest common subsequence (LCS) problem is the problem of fi ...

随机推荐

  1. .net语音播放,自定义播报文字

    // using System.Speech.Synthesis; SpeechSynthesizer synth = new SpeechSynthesizer(); // Configure th ...

  2. java之struts2之ServletAPI

    在之前的学习中struts2已经可以处理大部分问题了.但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI. 一种解耦合方式,一种耦合方式. 1. 解耦合方式 解耦合 ...

  3. c# $和@ 简化字符串格式化拼接

    int age=18; Console.WriteLine($"XiaoMing is \"{age}\" {{ years}} old"); Console. ...

  4. 有关Nodejs的一些插件介绍

    var child_process = require('child_process');这个可以执行cmd的命令 child_process.exec(cmdLine, function(error ...

  5. glfw内存泄露测试

    1. glfwInit()   和  glfwTerminate()  放在主线程  循环次数 1    内存   14M 循环次数100    46M 循环次数1000   49M 2.  glfw ...

  6. sql 注入风险

    目录 sql 注入风险 什么是sql注入呢? 查看sql注入风险 如何避免 sql 注入风险 pymysql 简单规避注入风险示列 sql 注入风险 什么是sql注入呢? 参考百度 查看sql注入风险 ...

  7. springboot整合tkmybatis

    tkmybatis是什么? tkmybatis是为了简化mybatis单表的增删改查而诞生的,极其方便的使用MyBatis单表的增删改查,在使用mybatis单表增删改查时,可以直接调用tkmybat ...

  8. day53-python之会话

    from django.shortcuts import render,redirect # Create your views here. import datetime def login(req ...

  9. QTableWidget数据表格

    void setRowHeight(int row, int height); //行高 void setVerticalHeaderLabels(const QStringList &lab ...

  10. python 版本号比较 重载运算符

    # -*- coding: utf-8 -*- class VersionNum(object): """ 版本号比较 默认版本以“.”分割,各位版本位数不超过3 例一: ...