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-白话事物
什么是事物,把一组逻辑放在一起作为一个单元来提交执行,这就是事物,这不是定义,大概是这么个意思 如果你留心的话,你会看到到处都有事物,到处都会提到ACID四个特性(原子性,一致性,隔离性,持久性) R ...
- STL与泛型编程-练习2-GeekBand
练习题目: struct Programmer{ Programmer(const int id, const std::wstring name): Id(id), Name(name){ } vo ...
- JZOJ100045 【NOIP2017提高A组模拟7.13】好数
题目 题目大意 首先有一个定义: 对于一个数,如果和它互质的数可以组成一个等差数列,那么这个数叫"好数". 现在给你一个数列,有三种操作: 1.询问一段区间内的好数的个数. 2.将 ...
- c语言学习笔记 - 枚举类型
今天学习了c语言的枚举类型的使用,可能是PHP里没使用过,开始看的时候还是觉得有点怪,后来做了下例子才理解,这里做个笔记记录一下. #include <stdio.h> enum anim ...
- 各ui库项目结构
饿了么的ui组件库 sass build:webpack配置文件 examples: element api的页面文档 packages: 放置组件 css放在./theme-chalk 下 src ...
- vue+element-ui 使用富文本编辑器
npm安装编辑器组件npm install vue-quill-editor –save 在components文件夹创建ue.vue组件,如下 ue.vue代码如下: <!-- 组件代码如下 ...
- codec engine工程中使用ccs下编译的lib库
原文地址:codec engine工程中使用ccs下编译的lib库--转作者:木子小白 这两天将dsp的算法程序放到ccs下,生成lib库文件 这样的好处就是: 1. 算法封装成lib库以后,看不到源 ...
- boxFilter in opencv
, -),bool normalize=true,int borderType=BORDER_DEFAULT) Smoothes image using box filter Parameters: ...
- ArrayList基础知识
ArrayList简介 ArrayList 的底层是数组队列,相当于动态数组.与 Java 中的数组相比,它的容量能动态增长.在添加大量元素前,应用程序可以使用ensureCapacity操作来增加 ...
- python-基础-练习和面试题
给程序传参数 import sys print(sys.argv) 运行结果: 列表推导式 所谓的列表推导式,就是指的轻量级循环创建列表 1. 基本的方式 2. 在循环的过程中使用if 3. 2个fo ...