leetcode1143 Longest Common Subsequence
"""
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的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
随机推荐
- 避免学习Linux走弯路
我并不是一位Linux老鸟.在学习Linux的一路上.也是走了很多弯路,踩了不少坑.而今日就把自己趟过的那些坑给我们总结出来,期望能给初学Linux的童靴们带来一丝丝的帮助吧.文采可能没有那么高深,只 ...
- (未解决)flume监控目录,抓取文件内容推送给kafka,报错
flume监控目录,抓取文件内容推送给kafka,报错: /export/datas/destFile/220104_YT1013_8c5f13f33c299316c6720cc51f94f7a0_2 ...
- Windows Android SDK下载安装,配置,异常问题解决教程
团队编程项目终于开始了,相信大家都在如火如荼的准备的当中,这里念半整理了一份还比较全面的关于 Android SDK的下载安装的教程,当然如果你说你们小组的实验环境选择的是Android studio ...
- SRS——打开 stream caster
按照默认的配置编译启动后,发现 stream caster 不起作用,启动时报如下警告: [-- ::][][] stream caster: off 原因是编译SRS时没有打开StreamCaste ...
- 对于JAVA语言的一点理解
java作为一门面向对象的语言,现在常常被用于企业服务器端的后台开发.同时,C语言可能更多地是用于嵌入式的开发,所谓的嵌入式就是航天飞机上的设备软件之类的东西.但是,我逐渐发现,我们平时所说的java ...
- 139、Java内部类之使用this访问外部类属性
01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; class ...
- PaperReading20200223
CanChen ggchen@mail.ustc.edu.cn AdaBatch Motivation: Current stochastic gradient descend methods u ...
- Linux之网络配置
Linux系统网络设备配置文件在/etc/sysconfig/network-scripts/目录中,可使用Vim编辑器编辑网卡配置文件,来配置网络服务,在RHEL7中,网卡配置文件以前缀ifcfg开 ...
- java并发初探CountDownLatch
java并发初探CountDownLatch CountDownLatch是同步工具类能够允许一个或者多个线程等待直到其他线程完成操作. 当前前程A调用CountDownLatch的await方法进入 ...
- java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )
一.课堂测试 1.课堂测试:花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求 (1)减法结果不能为负数 (2)乘法结果不得超过一百,除法结果必须为整数 (3)题目避免重复: (4)可 ...