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 ...
随机推荐
- 开发框架-APP:Hybird App
ylbtech-开发框架-APP:Hybird App Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具“Native App良好用户交互体 ...
- Apache JMeter配置、安装
一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后来又扩充了其它的功能.j ...
- Centos 的计划任务 crontab
使用计划任务! crontab命令主要有3个参数: -e :编辑用户的crontab. -l :列出用户的crontab的内容. -r :删除用户的crontab的内容. 执行crontab -e,将 ...
- 逻辑斯蒂回归(Logistic Regression)
逻辑回归名字比较古怪,看上去是回归,却是一个简单的二分类模型. 逻辑回归的模型是如下形式: 其中x是features,θ是feature的权重,σ是sigmoid函数.将θ0视为θ0*x0(x0取值为 ...
- 第10课 C++异常简介
1. try-catch语句 (1)try语句处理正常代码逻辑 (2)catch语句处理异常情况 (3)try语句中的异常由对应的catch语句处理 (4)C++通过throw语句抛出异常信息 2. ...
- 「CQOI2016」K 远点对
/* 考虑暴力 可以n ^ 2枚举点对 然后用一个容量为2k的小根堆来维护第k大 kd-tree呢就是来将这个暴力优化, 每次先找远的并且最远距离不如堆顶的话就不继续找下去 貌似挺难构造数据卡的 */ ...
- MS SQL Server 定时任务实现自动备份
SQL Server Express 版本是没有SQL 代理服务的,从而导致不能使用SQL Server的定时自动备份功能.真心感觉这就是一个坑,虽然Express是学习的版本,但是精简的也太多了.另 ...
- CentOS 7.4 安装部署 iRedMail 邮件服务器
在公司部署了一套开源的邮件网关Scrollout F1用来测试,由于Scrollout F1需要使用IMAP协议连接到邮件服务器上的隔离邮箱,抓取GOOD和BAD文件夹里的邮件进行贝叶斯学习,但公司的 ...
- restful 注解 总结 (比较完整的):http://www.xuetimes.com/archives/388 , https://www.cnblogs.com/chen-lhx/p/5599806.html
参考1: http://www.xuetimes.com/archives/388 参考2: https://www.cnblogs.com/chen-lhx/p/5599806.html 参考 ...
- 使用.htaccess文件
禁止对无索引文件的目录进行文件列表展示 默认情况下,当我们访问网站的某个无索引文件(如index.html,index.htm或 index.php)目录时,服务器会显示该目录的文件和子目录列表,这是 ...