LintCode刷题笔记-- LongestCommonSquence
标签:动态规划
题目描述:
Given two strings, find the longest common subsequence (LCS).
Your code should return the length of LCS.
解题思路:
这一题是非常经典的动态规划问题,在解题思路上可以按照经典的动态规划的解法,这是在系统学习动态规划之后第一个解决的LintCode上的问题:
1.子问题划分
给出两个字符串的A,B,两个字符串长度分别为lenA,lenB,求出两个字符串的LCS:
这划分为子问题:A.subString(0,0) 与B的LCS,在此基础上A.subString(0,1)与B的LCS,依次类推,可以得到A.subString(0,lenA-2), A.subString(0,lenA-1) 与B的LCS。 按照A的方法也同样可以对B在长度上进行分解。这样可以形成字符串A与B长度为lenA*lenB的矩阵,此矩阵为记录状态的“备忘录”。
2.初始状态的定义
对于LCS矩阵的初始状态,对于第一行与第一列相对应的意义为,A与B的第一个元素作为一个长度为1的字符串与对方是否存在公共字符,若存在,所在位置坐标已经后续坐标全部置为1.
3.问题与子问题解的关系
dp[i][j]:字符串的A的子串dp(0,i)与字符串B的子串dp(0,j)之间LCS的长度
dp[i][j]的取值:当A[i] == B[j],A(0,i)与B(0,j)之间的LCS会较比A(0,i-1)与B(0,j-1)多1,因为多了1位公共字符,LCS的长度自然会增加1。
当A[i]!=B[j], A(0,i)与B(0,j)之间的LCS会选择先前公共子串更多的部分作为下一步求解
4.边界条件
当达到A,B两者的最大长度时结束
5.参考代码:
public int longestCommonSubsequence(String A, String B) {
int lenA = A.length();
int lenB = B.length(); if(lenA == 0 || lenB == 0){
return 0;
} int[][] dp = new int[lenA][lenB]; if(A.charAt(0)==B.charAt(0)){
dp[0][0] = 1;
} for(int i = 1; i < lenA; i++){
if(B.charAt(0)==A.charAt(i)){
dp[i][0] = 1;
}else{
dp[i][0] = dp[i-1][0];
}
}
for(int j = 1; j < lenB; j++){
if(A.charAt(0)==B.charAt(j)){
dp[0][j] = 1;
}else{
dp[0][j] = dp[0][j-1];
}
} for(int i = 1; i<lenA; i++){
for(int j = 1; j<lenB; j++){
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
if(A.charAt(i) == B.charAt(j)){
dp[i][j] = dp[i-1][j-1]+1;
} }
}
return dp[lenA-1][lenB-1];
}
LintCode刷题笔记-- LongestCommonSquence的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- Spring注解驱动(上)
记录常用的spring注解 1.@Configuration 和 @Bean spring中可以使用xml 的方式进行配置, 也可以使用 @ Configuration 来指定一个类为配置类, 并使用 ...
- linux中断处理-顶半部(top half)和底半部(bottom half) -转
原文:http://rensanshi.blog.163.com/blog/static/21395510820136282224877/ 设备的中断会打断内核中进程的正常调度和运行,系统对更高吞吐率 ...
- 11-1-break-continue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 引入Vue.js实现vue效果
拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...
- thymeleaf报错元素类型必须由匹配的结束标记终止
spring boot 1.x 版本中thymeleaf报错元素类型 “link” 必须由匹配的结束标记 “</link>” 终止解决办法: 1.在pom.xml文件中添加: <de ...
- vue:element-ui输入框绑定回车事件
参考: https://segmentfault.com/q/1010000011347642 https://weiku.co/article/297/ vue监听input输入框的回车事件:key ...
- [Swoole系列入门教程 3] 心跳检测
一.Swoole 的4大知识点: 1.TCP/UDP服务器 2.微服务 3.协程 二.同步与异步: 同步买奶茶:小明点单交钱,然后等着拿奶茶: 异步买奶茶:小明点单交钱,店员给小明一个小票,等小明奶茶 ...
- python中defaultdict方法的使用
默认值可以很方便 众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键 ...
- 第一章 使用开发者模式快速入门 Odoo 12
本文为最好用的免费ERP系统Odoo 12开发手册系列文章第一篇. Odoo提供了一个快速应用开发框架,非常适合创建商业应用.这类应用通常用于保留业务记录,增删改查操作.Odoo 不仅简化了这类应用的 ...
- python 日记 day5 字典
一.数据类型分为: 1.不可变数据类型:元组,bool,int,str (不可更改的又叫可哈希) 2.可变数据类型 :list,dict ,set (可变的又叫不可哈希) 字典: 优点:1.可二分 ...