标签:动态规划

题目描述:

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的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  3. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  4. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  5. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  6. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  7. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  8. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

  9. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

随机推荐

  1. css 阴影设置box-shadow

    box-shadow: 1px 2px 5px 6px #888888 inset 1px: 必须,水平阴影的位置, 负值为左 2px: 必须,垂直阴影的位置, 负值为上; 5px: 可选,模糊距离; ...

  2. java笔试之求最小公倍数

    正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数. package test; import java.util.Scanner; publ ...

  3. 解决linux机器克隆后eth0不见的问题

    克隆机器之后,两个几的物理地址和ip地址是一样的,导致克隆的机器网络不可用,可以通过通过如下步骤修改: 通过ifconfig –a   命令可查看所有的ip地址配置. 通过这个命令可以发现有一个eth ...

  4. redis教程(三)-----redis缓存雪崩、缓存穿透、缓存预热

    缓存雪崩 概念 缓存雪崩是由于原有缓存失效(过期),新缓存未到期间.所有请求都去查询数据库,而对数据库CPU和内存造成巨大压力,严重的会造成数据库宕机.从而形成一系列连锁反应,造成整个系统崩溃. 解决 ...

  5. 用wix制作属于自己的Flash网站

    Wix 制作属于自己的Flash网站 Wix 是一款新兴的在线应用程序,它可以帮助用户轻松的创建出绘声绘色的Flash网站,而不需要任何相关的专业知识.Wix 是一家位于以色列的Startup开发的一 ...

  6. Django-rest Framework(五)

    把十大接口做完了才能更好的了解后面的视图类 1.(重点)二次封装Response;自定义APIResponse继承Response,重写 ____init____方法 from rest_framew ...

  7. 删除n天前的文件或文件夹 bat批处理

    @echo off @echo deleting... FORFILES /p "D:\a" /D -1 /C "cmd /c echo deleting @file . ...

  8. 微信小程序之组件的集合(二)

    继续微信小程序开发的学习,继续跟着老师的讲课思路来学习,继续开发项目中所用到的组件 一.导航栏navi组件的开发 1.新建组件的文件结构 这个就是先新建目录navi.然后在navi文件夹中新建comp ...

  9. 一句话介绍python线程、进程和协程

    一.进程: Python的os模块封装了常见的系统调用,其中就包括fork.而fork是linux常用的产生子进程的方法,简言之是一个调用,两个返回. 在python中,以下的两个模块用于进程的使用. ...

  10. idea小操作

    1.IDEA 实用功能Auto Import:自动优化导包(自动删除.导入包) 2.设置System.out.println();等快捷键 3.将idea的背景修改为图片 4.Linux ifconf ...