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. vsCode 代码不高亮显示的问题

    安装Vetur插件 1.点击左侧菜单的扩展-->搜索Vetur-->点击安装-->安装完成重启vsCode  --->

  2. Java 模拟http请求

    package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  3. 五分钟快速掌握RPC原理及实现

    随着公司规模的不断扩大,以及业务量的激增,单体应用逐步演化为服务/微服务的架构模式, 服务之间的调用大多采用rpc的方式调用,或者消息队列的方式进行解耦.几乎每个大厂都会创建自己的rpc框架,或者基于 ...

  4. Dapper结合Repository模式的应用

    Dapper结合Repository模式的应用,包括如何在数据访问层(DAL)使用Dapper组件. Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle.MS SQL ...

  5. numpy array分割-【老鱼学numpy】

    有合并,就有分割. 本节主要讲述如何通过numpy对数组进行横向/纵向分割. 横向/纵向分割数组 首先创建一个6行4列的数组,然后我们对此数组按照横向进行切割,分成3块,这样每块应该有2行,见例子: ...

  6. hadoop解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题

    先看下自己的JAVA_HOME里面有没有空格目录,如果有的话,先把JAVA_HOME换个没空格的位置. 在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序 ...

  7. linux学习:归档,备份及进程相关命令用法整理

    指令:tar.zip.gzip.unzip.rsync.scp.ps.kill.nohup 压缩 tar 归档命令,不具备压缩功能 tar -zcvf test.tar.gz  test/     # ...

  8. swust oj 983

    利用二叉树中序及后序遍历确定该二叉树的先序序列 1000(ms) 10000(kb) 2606 / 4908 已知二叉树的中序和先序遍历可以唯一确定后序遍历.已知中序和后序遍历可以唯一确定先序遍历,但 ...

  9. linux for循环 fork() 产生子进程

    #include <sys/types.h> #include <unistd.h> #include<stdio.h> int main() { for(int ...

  10. 记一次FileZillaServer提权

    前段时间检测一个企业网站,在检测该企业的一个下属公司的网站时通过用户名admin和密码123456进入了后台,后台目录就是公司汉语拼音+admin,诸如xxxadmin这种形式的.在后台通过“产品图片 ...