97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
//dp[i][j]: s3[i+j-1] can be interleaved by s1[0..i], s2[0..j]
//dp[i][j] = dp[i-1][j] if s3[i+j-1]==s1[i]
// = dp[i][j-1] if s3[i+j-1]==s2[j]
//so traverse i,j,k from small to large
int len1 = s1.length();
int len2 = s2.length();
if(len1+len2!=s3.length()) return false;
vector<vector<bool>> dp(len1+, vector<bool>(len2+, false)); //initial status
dp[][] = true; //0 means null string
for(int j = ; j <= len2; j++){
dp[][j] = dp[][j-] && s2[j-]==s3[j-];
}
for(int i = ; i <= len1; i++){
dp[i][] = dp[i-][] && s1[i-]==s3[i-];
} //state transfer
for(int i = ; i<= len1; i++){
for(int j = ; j <= len2; j++){
dp[i][j] = (dp[i][j-] && s2[j-]==s3[i+j-]) || (dp[i-][j] && s1[i-]==s3[i+j-]);
}
}
return dp[len1][len2];
}
};
97. Interleaving String (String; DP)的更多相关文章
- leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]
1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...
- hdu6194 string string string
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6194 题目: string string string Time Limit: 2000/10 ...
- HDU 6194 string string string(后缀数组+RMQ)
string string string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- 关于 Dictionary<string,string>,和List<T>在View的使用
在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...
- alibaba fastjson List<Map<String, String>>2Str
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- getParameterMap()的返回值为Map<String, String[]>,从其中取得请求参数转为Map<String, String>的方法如下:
直接遍历报错:[Ljava.lang.String;@44739f3f Map<String, String> tempMap = new HashMap<String, Strin ...
- The constructor User.Student(String, String, String) is not visible
项目:蒙文词语检索 日期:2016-05-01 提示:The constructor User.Student(String, String, String) is not visible 出处:Db ...
- 从为什么String=String谈到StringBuilder和StringBuffer
前言 有这么一段代码: public class TestMain { public static void main(String[] args) { String str0 = "123 ...
随机推荐
- java web程序 jdbc连接数据库错误排查方法
学习jsp.我遇到了麻烦,我总是看不懂500错误,因为每次都显示整个页面的错误,都是英文 我看不懂,后来,把他弄烦了,我也烦了,比起学习java.那个异常可以很简单的就知道.现在解决 了第一个问题,5 ...
- H-Index II @python
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html
下载 在官方网站 http://www.aditus.nu/jpgraph/ 下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的. 安装 将下载的得到的jpgraph压缩文 ...
- js之ActiveX控件使用说明 new ActiveXObject()
什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...
- Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263 git 路径 https://github.com/wangpanjun/d ...
- multiprocess模块---进程---进程队列
首先明白几个概念: 同步:做完一件事情,再做另外一件事情 异步:做一件事情的时候,可以再做另外一件事情 阻塞:recv sleep accept input recvfrom 非阻塞:没有遇见上面这 ...
- django-重写User模型
User模型有很多功能,验证什么的,重写需要满足下面的功能(基本上写注释的地方都是需要的) 开始: 创建一个重写user的app, 记得注册app startapp newauth from djan ...
- ldap快速配置
1.[yum lamp环境] yum -y install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-co ...
- centos7.3nginx配置二级域名过程
nginx1.10.2 1先检查 /etc/nginx/nginx.conf 是否include conf.d include /etc/nginx/conf.d/*.conf; 默认都是包含的,如 ...
- NPOI2.2.0.0实例详解(十一)—向EXCEL插入图片
--------------------- 本文来自 天水宇 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xxs77ch/article/details/50553 ...