[LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS).
Example
Example 1:这个题目思路是利用dynamic programming,用二维的,mem[l1 + 1][l2 + 1] # mem[i][j] 去表示s1的前i个characters与s2的前j个characters的LCS的length。
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"
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的更多相关文章
- [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 ...
- [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 ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 虚拟机Ubuntu16.04无法进入图形界面 The system is running in low-graphics mode
安装的虚拟机Ubuntu16.04 64位本可以正常使用,在安装了许多软件包(caffe)后不知哪里配置出现问题,出现The system is running in low-graphics mod ...
- python第10天(下)
https://www.cnblogs.com/zingp/p/6863170.html 一:IO模型介绍 IO发生时涉及的对象和步骤 对于一个网络IO(network IO),它会涉及到两个系统对 ...
- 【C++ Primer | 09】容器适配器
一.stack s.push(): 向栈内压入一个成员: s.pop(): 从栈顶弹出一个成员: s.empty(): 如果栈为空返回true,否则返回false: s.top(): 返回栈顶,但不删 ...
- 20172328 2018-2019《Java软件结构与数据结构》第三周学习总结
20172328 2018-2019<Java软件结构与数据结构>第三周学习总结 概述 Generalization 本周学习了第五章:队列.主要内容包含队列的处理过程.如何用对例如求解问 ...
- Typescript高级类型与泛型难点详解
最近做的TS分享,到了高级类型这一块.通过琢磨和实验还是挖掘出了一些深层的东西,在此处做一下记录,也分享给各位热爱前端的小伙伴. 其实在学习TS之前就要明确以下几点: 1. typescrip ...
- HttpConnection
1.HttpConnection 用于接受和发送网络数据 网络操作必须新开个子线程执行,否则会出现 ANR(Application Not Response) 应用无响应异常 Get: /** * 通 ...
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- python dgango简介 安装 多表查询
一.socket服务器 import socket sk=socket.socket() sk.bind(("127.0.0.1",8890)) sk.listen() while ...
- php发送邮箱
/** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subjec ...
- Vue 前端面试题
Vue 前端面试题 1. 说一下 Vue 的双向绑定数据的原理 vue 实现数据双向绑定主要是:采用数据劫持结合“发布者 - 订阅者”模式的方式,通过 Object.defineProperty() ...