"""
Given two strings text1 and text2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
""" """
提交显示wrong answer,但在IDE是对的
其实这个代码很冗余,应该是 超时
"""
class Solution1:
def longestCommonSubsequence(self, text1, text2):
res1, res2 = 0, 0
i, j = 0, 0
while i < len(text1) and j < len(text2):
if text1[i] == text2[j]:
res1 += 1
i += 1
j += 1
else:
if len(text1) - i < len(text2) - j: #第二遍循环是为了处理这个
i += 1
else:
j += 1
i, j = 0, 0
while i < len(text1) and j < len(text2):
if text1[i] == text2[j]:
res2 += 1
i += 1
j += 1
else:
if len(text1) - i > len(text2) - j:
i += 1
else:
j += 1
res = max(res1, res2)
return res """
经典的动态规划,用一个二维数组,存当前的结果
如果值相等:dp[i][j] = dp[i-1][j-1] + 1
如果值不等:dp[i][j] = max(dp[i-1][j], dp[i][j-1]) 左边和上边的最大值
在矩阵 m行n列容易溢出,这点很难把握
目前的经验,每次严格按照行-列的顺序进行
"""
class Solution:
def longestCommonSubsequence(self, text1, text2):
n = len(text1)
m = len(text2)
dp = [[0]*(m+1) for _ in range(n+1)] #建立 n+1行 m+1列矩阵,值全为0
for i in range(1, n+1): #bug 内外循环层写反了,导致溢出,n+1 * m+1 矩阵
for j in range(1, m+1):
if text1[i-1] == text2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[-1][-1]

leetcode1143 Longest Common Subsequence的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  3. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  4. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  5. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  7. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

  8. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  9. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

随机推荐

  1. 小程序云开发使用where查询遇到的问题

    想用小程序云开发的where查询,结果不论输入什么都是不报错,开始没注意,后来发现输入数据库中有的数据时,给打印出来查询成功,输入数据库中没有的数据时,也会得到一个集合,只不过这个集合的长度为0而已. ...

  2. 「POI2011」Meteors

    「POI2011」Meteors 传送门 整体二分,树状数组实现区间修改单点查询,然后注意修改是在环上的. 参考代码: #include <cstdio> #include <vec ...

  3. [转]简单总结一下解决 添加 inline-block 后多出来的空隙

    添加 inline-block 后: 查询.借鉴的原网址:http://www.zhangxinxu.com/wordpress/?p=2357 html 结构: <ul class=" ...

  4. delphi IdSMTP发送邮件

    TIdPOP3组件简介 TIdPOP3 是用来接收邮件服务器的邮件信息到用户端的一个组件.它实现了RFC 1939协议. 在使用TIdPOP3组件时需设置它的几个成员属性. Host :指定邮件服务器 ...

  5. python中对闭包的理解

    运行环境声明:本人的代码在sublime text 3中写的,可以Ctrl+b运行.python版本是python3.6.如果您直接运行的,请自觉加入if __name__ == '__main__' ...

  6. 简述javascript的解析与执行

    我们知道浏览器中javascript程序的执行是基于变量与函数的.那么浏览器是如何保存数据,又是如何执行的呢?今天我们一起来探究一下! 0.写在前 最新的 ECMAScript 标准定义了 8 种数据 ...

  7. AJAX的表单请求POST请求方式

    表单数据的提交 action : 数据提交的地址,默认是当前页面 method : 数据提交的方式,默认是get方式 post: 把数据名称和数据值用=连接,如果有多个的话,那么他会把多个数据组合用& ...

  8. bzoj 4475: [Jsoi2015]子集选取

    233,扒题解的时候偷瞄到这个题的题解了,,GG 暴力发现是2^(nm),然后就是sb题了 #include <bits/stdc++.h> #define LL long long us ...

  9. 3(计算机网络)ifconfig:最熟悉又陌生的命令行

    当面试听到这个问题的时候,面试者常常会觉得走错了房间.我面试的是技术岗位啊,怎么问这么简单的问题? 的确,即便没有专业学过计算机的人,只要倒腾过电脑,重装过系统,大多也会知道这个问题的答案:在 Win ...

  10. redis、mongodb、memcache安装好后设置开机自启动

    vim /etc/rc.d/rc.local /usr/local/mongodb/bin/mongod --smallfiles /usr/local/bin/redis-server/usr/lo ...