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. spring security 简单应用

    Pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...

  2. tensorflow添加层-【老鱼学tensorflow】

    本节主要定义个添加层的函数,在深度学习中是通过创建多层神经网络来实现的,因此添加层的函数会被经常用到: import tensorflow as tf def add_layer(inputs, in ...

  3. Scala变量| 流程控制

    Scala 是 Scalable Language 的简写,是一门多范式(编程的方式)的编程语言 Scala是一门以java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的 ...

  4. sbadmin表单事件

    Form表单 自定义表单 <from action="" method="'><!---      这里可以用表单组件快速生成表单元素哦        ...

  5. JS对象的拷贝

    1:对数据进行备份的时候,如果这个数据是基本的数据类型,那么很好办,通过赋值实现复制即可. 赋值与浅拷贝的区别 var obj1 = { 'name' : 'zhangsan', 'age' : '1 ...

  6. 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Auxiliary Project 完全背包. #include<stdio.h> #include<iostream> #include<string.h> ...

  7. 【转】Spring总结以及在面试中的一些问题

    [转]Spring总结以及在面试中的一些问题. 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建 ...

  8. 06_ for 练习 _ 年利率

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. ECMA Script 6_简单介绍

    ECMAScript 6 ECMA 组织 前身是 欧洲计算机制造商协会 指定和发布脚本语言规范,标准在每年的 6 月份正式发布一次,作为当年的正式版本 这样一来,就不需要以前的版本号了,只要用年份标记 ...

  10. ActivityRouter 框架简单实用

    ActivityRouter组件化开发小助手用法如下: 跟目录build.gradle dependencies { // activityRouter classpath 'com.neenbeda ...