[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 ...
随机推荐
- 在Pythonfor循环中如何获取循环次数?
在Python的for循环里,循环遍历可以写成: for item in list: print item 它可以遍历列表中的所有元素,但是有什么方法可以知道到目前为止我循环了多少次? 想到的替代方案 ...
- 期货大赛项目|八,ueditor的应用
百度开发的富文本编辑器还是很不错的,可以已经不维护了 下载ueditor1_4_3_3-utf8-net放到项目中 找到net文件夹下config.json 修改以下两行 "imageUrl ...
- 在Linux下用gcc编译hello world
1. 确保Linux系统里已经装好了gcc 测试:输入gcc后是如下的结果就说明已经安装成功 2. 创建HelloWorld.c 使用 touch 创建一个空文件; 用vim编辑 按下A或者I 插入 ...
- [BOI2007]Mokia 摩基亚
Description: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如"用户C的位置在哪?"的问题,精确到毫 ...
- [Code+#4]最短路 解题报告
Luogu · 传送门 Orz THU众大佬,lct(注意不是link-cut-tree,是一个大佬) 这道题很容易让人联想到 最短路,但是最短路需要先 建图: 暴力建出所有边的算法显然是不可行的,因 ...
- mysql Navicat 导入导出
1.导出数据库: 打开Navicat ,在我们要导出的数据库上右击鼠标,然后弹出的快捷菜单上点击“转储SQL 文件”,(有些版本, 会有子菜单,在再次弹出的子菜单项中选择第一个“数据跟结构”) ...
- HTML5_canvas_像素操作_图片马赛克_图片反相
canvas 像素操作 像素,即像素点,一个像素只有一个颜色 100*100 的 px 的屏幕区域有 100*100*4 个像素点,即 width*height*4 rgba(0, 0, 0, 1); ...
- BOM 浏览器对象模型_渲染引擎_JavaScript 引擎_网页加载流程
1. 浏览器核心的两个组成部分 渲染引擎 将网页代码渲染为用户视觉可以感知的平面文档 分类: Firefox Gecko 引擎 Safari WebKit 引擎 Chrom ...
- java生成/解析二维码
package a; import java.awt.BasicStroke; import java.awt.Graphics; import java.awt.Graphics2D; import ...
- convert(varchar(10),字段名,转换格式
sql 时间转换格式 ) convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时 ...