Given two strings, find the longest common subsequence (LCS).

Example

Example 1:
Input: "ABCD" and "EDCA"
Output: 1 Explanation:
LCS is 'A' or 'D' or 'C' Example 2:
Input: "ABCD" and "EACB"
Output: 2 Explanation:
LCS is "AC"
这个题目思路是利用dynamic programming,用二维的,mem[l1 + 1][l2 + 1] # mem[i][j] 去表示s1的前i个characters与s2的前j个characters的LCS的length。
function : mem[i][j] = max(mem[i][j - 1], mem[i - 1][j]) if s1[i - 1] != s2[j - 1]
              max(mem[i][j - 1], mem[i - 1][j], mem[i - 1][j - 1] + 1) if s1[i - 1] == s2[j - 1]
initialize : mem[i][0] = mem[0][j] = 0 Code:
class Solution:
def LCS(self, s1, s2):
l1, l2 = len(s1), len(s2)
mem = [[0] * (l2 + 1) for _ in range(l1 + 1)]
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
temp = 1 if s1[i - 1] == s2[j - 1] else 0:
mem[i][j] = max(mem[i - 1][j], mem[i][j - 1], mem[i - 1][j - 1] + temp)
return mem[l1][l2]
 

[LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  4. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  5. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  8. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. python selenium Chrome模拟手机浏览器

    在做移动端页面测试时可以利用Chrome mobile emulation 辅助完成页面的适配问题,但是目前手机市场上的型号居多我们也没有办法通过人工的模式一一的去适配,所以这里考虑到通过自动化的模式 ...

  2. Visual Studio 2013 突然不高亮,编译报错

    同事的电脑,估计是windows更新失败的原因,C盘容量减小到不到1G,VS的高亮也坏了,重启后硬盘容量增加了但是仍然不高亮. 解决办法记录一下: 重置下. 开始菜单 -->所有程序--> ...

  3. RxJS操作符(二)

    一.Observable的性质 三种状态:nex, error, complete 进入到Error状态: ) .filter(val=>{ throw '出错了' }) .take() .re ...

  4. mysql的坑

    mysql安装报错: 1.The service already exists! The current server installed: 因为mysql卸载不完全. 解决方法: C:\window ...

  5. UOJ#218. 【UNR #1】火车管理 线段树 主席树

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ218.html 题解 如果我们可以知道每次弹出栈之后新的栈顶是什么,那么我们就可以在一棵区间覆盖.区间求和 ...

  6. echart折线图系列一:折线图基本配置

    引入echart插件 页面上准备一个容器:<div id="box" style="height:400px;width: 800px;padding: 20px& ...

  7. linux打包文件和压缩

    1.解压:tar -zxvf archive.tar , unzip file1.zip 2.压缩:tar -cvfz archive.tar.gz dir1 , zip file1.zip file ...

  8. mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)

    1.进入终端命令行 (1)输入:cd .ssh/ 进入到.ssh后,输入ls,会输出“known_hosts”,如果没有创建过rsa的话 (2)输入:man ssh-add (3)输入:ssh-key ...

  9. Vue(二十六)父子组件通信

    今天写了一个分页公共组件,就出现了父子组件通信的问题,今天来总结下我遇到的父子组件通信问题 一.子组件调取父组件的数据或方法 (1)props 想要把父组件的值,传到子组件中,使用props 比如你在 ...

  10. 查找已连接过的wifi密码

    无意之间看到能破解已连接过的wifi密码的诀窍,赶紧存储下来. 1. 首先打开终端 2. 在没有网路的情况下输入: netsh wlan show profiles 结果如下(自己的): 这些‘用户配 ...