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 ...
随机推荐
- 07_springmvc校验
一.概述 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrol ...
- webpack 处理图片文件
1. 安装 file-loader html-loader npm install file-loader html-loader --save-dev 其中html-loader生效需配合 html ...
- svn里update以后还是有红色的感叹号怎么办
不用那么麻烦,直接还原就行了,客户端是TortoiseSVN的话,在该文件或文件夹上点右键,选择TortoiseSVN——revert有时还原之后系统反应没那么快,还是显示红色感叹号,刷新几下就正常了 ...
- 通过js 存取cookie
//存cookie function setCookie(name,value){ var Days = 30; var exp = new Date(); exp.setTime(exp.getTi ...
- Vue 提示框组件
OK,首先看看效果: 一.子组件(alert.vue) <template> <transition name="alert"> <div class ...
- ms12-020复现-xp蓝屏
漏洞名:MS12-020(全称:Microsoft windows远程桌面协议RDP远程代码执行漏洞) 介绍:RDP协议是一个多通道的协议,让用户连上提供微软终端机服务的电脑. windows在处理某 ...
- 设置mysql二进制日志过期时间
((none)) > show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | ...
- 03Redis入门指南笔记(事务、生存时间、排序、消息通知、管道)
一:事务 1:概述 Redis中的事务(transaction)是一组命令的集合.事务同命令一样都是Redis的最小执行单位,一个事务中的命令要么都执行,要么都不执行. 事务的原理是是先将属于一个事务 ...
- centos7.6 安装jdk1.8
1. 下载 jdk-8u211-linux-x64.tar.gz文件. 2. 创建/opt/soft目录,# cd /opt, # mkdir soft, #tar -zxvf jdk-8u211- ...
- 华为云DevCloud一枝独秀
DevOps,是Development和Operations的组合词,是指一组过程.方法与系统的统称,用于促进开发.技术运营和质量保障部门之间的沟通.协作与整合.DevOps是一种重视“软件开发人员( ...