LintCode刷题笔记-- InterLeaving
标签:
动态规划
解题思路
1. 这道题最重要的是,存在三个字符串,但是并不需要两个二维矩阵来进行解,因为可以使用i+j-1来代表s3的下标,这样就可以通过i和j来遍历s3了。因为对于任何一个合法的交叉字符串都会有,s3(i+j-1)=s1(i-1) 或者s3(i+j-1) = s2(j-1)
2. 所以对于动态规划矩阵就可以在s1,s2这两个向量上进行分解,有dp[i][j], 其所代表的意义的是在s3(i+j-1)的位置上是否存在有s1(i)或者s2(j)与其相等,
3.如果结果返回true,证明是一对交叉字符串,对于矩阵中的每一个点都会存在其上方或者左方有一个点为true。因为在s3中的前一个节点会有s1或者s2中的一个点来进行匹配,也可能存在两者都相等,这样的情况中,左方和上方的解应当都为true。
4. 对于初始状态,也是最懵逼的,看答案想了很久没有明白:分别遍历s1与s2,与s3进行比较,找出s1或者s2的前几项与s3。找出矩阵的入口,因为如果两个字符串为合法的交叉字符串的话,至少dp[i][0]或者dp[0][j]中至少有一个序列中存在两个true,其中dp[0][0]为true。
对于dp的解法:
s1 = a1, a2 ........a(i-1), ai
s2 = b1, b2, .......b(j-1), bj
s3 = c1, c3, .......c(i+j-1), c(i+j)
定义 match[i][j] 意味着,S1的(0, i)和S2的(0,j),匹配与S3的(i+j)
如果 ai == c(i+j), 那么 match[i][j] = match[i-1][j], 等价于如下字符串是否匹配。
s1 = a1, a2 ........a(i-1)
s2 = b1, b2, .......b(j-1), bj
s3 = c1, c3, .......c(i+j-1)
同理,如果bj = c(i+j), 那么match[i][j] = match[i][j-1];
5.参考代码:
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()) return false;
boolean[][] dp = new boolean[s1.length()+1][s2.length()+1];
dp[0][0] = true;
for(int i=1; i<=s1.length(); i++){
if(s1.charAt(i-1)==s3.charAt(i-1)&&dp[i-1][0]==true){
dp[i][0] = true;
}
}
for(int j = 1; j<=s2.length(); j++){
if(s2.charAt(j-1)==s3.charAt(j-1)&&dp[0][j-1]==true){
dp[0][j] = true;
}
}
for(int i = 1; i<=s1.length(); i++){
for(int j = 1; j<=s2.length(); j++){
if(s1.charAt(i-1)==s3.charAt(i+j-1)&&dp[i-1][j]||
s2.charAt(j-1)==s3.charAt(i+j-1)&&dp[i][j-1]){
dp[i][j] = true;
}
}
}
return dp[s1.length()][s2.length()];
}
LintCode刷题笔记-- InterLeaving的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- 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 ...
随机推荐
- ORA-01790: 表达式必须具有与对应表达式相同的数据类型
出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同.
- js时间比较大小,时间加减
第一种: //时间类比较 startTime= new Date(Date.parse(starttime)); endTime=new Date(Date.parse(endTime)); //进行 ...
- C语言内存布局简记待补充
C语言存储类型总结内存操作函数总结 用于自己学习和记录 1. void *memset(void *s, int c, size_t n); #include <string.h> 功能: ...
- 跟我一起学习webpack(一)
跟我一起打包我们的第一个应用 第一步安装webpack 接下来我们新建文件 //add-content.js export default function(){ document.write('he ...
- c语言学习笔记 关于double
今天做了个简单的例子,由于没有使用正确的数据类型导致出错,下面是记录 #include <stdio.h> int main(void){ int i; double sum; doubl ...
- ArcMap中给点shp添加字段后,shp文件破坏无法打开
这两天遇到一个奇怪的问题,在整理项目中的建筑物数据时发现,有几个图层进行字段添加后出现问题,shp文件被损坏了.这问题很隐蔽,给shp添加字段后不报错,进行赋值,报错如下: 但是无论是选择“是”还是“ ...
- 深入浅出 Java Concurrency (21): 并发容器 part 6 可阻塞的BlockingQueue (1)[转]
在<并发容器 part 4 并发队列与Queue简介>节中的类图中可以看到,对于Queue来说,BlockingQueue是主要的线程安全版本.这是一个可阻塞的版本,也就是允许添加/删除元 ...
- DVWA 之low级别sql注入
将Security level设为low,在左侧列表中选择“SQL Injection”,然后在右侧的“User ID”文本框中输入不同的数字就会显示相应的用户信息. 我们首先需要判断这里所传输的参数 ...
- 学习Python笔记---变量和简单数据类型
首先声明,这个是个人在自学的一些笔记,因为是小白,刚接触Python,之前也没有过类似的经验,所以很多东西对于其他人来说可能是小白级别的,写出来没有其他的意思就是自己整理然后记录一下,顺便分享出来,而 ...
- 1.开始Spring
1 对Spring的认识 为了实现控制反转,我们可以想象java创建了一个工厂类,工厂类可以去读取配置文件 比如一个.property文件.通过这个文件中的配置,反射创建一些对象,当外界需要某个类的对 ...