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 ...
随机推荐
- 操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统)
ylbtech-操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统) iOS是由苹果公司开发的移动操作系统.苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计 ...
- sping mvc+uploadify 上传文件大小控制3部曲
页面使用uploadify 上传控件,使用spring CommonsMultipartipartResolver , 反向代理nginx nginx 配置文件 client_max_body_siz ...
- php实现socket
一.Socket 简介 1.socket只不过是一个数据结构. 2.使用这个socket数据结构去开始一个客户端和服务器之间的会话. 3.服务器是一直在监听准备产生一个新的会话.当一个客户端连接服务器 ...
- git log --author详解,这个是个模糊匹配
git log --author=authorname --author=<pattern>, commits whose author matches any of the given ...
- tensorflow定义神经网络损失函数MSE
import numpy as np import tensorflow as tf y_pred = np.array([[1], [2], [3]],dtype=np.float32) y_rea ...
- ubuntu18.04 安装hadoop 2.7.3+hive 2.3.4
1. 安装hadoop 详细请参见本人的另外一片博文<Hadoop 2.7.3 分布式集群安装> 2. 下载hive 2.3.4 解压文件到/opt/software -bin.tar.g ...
- 文件系统性能测试--iozone
iozone 一个文件系统性能评测工具,可以测试Read, write, re-read,re-write, read backwards, read strided, fread, fwrite, ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #17 如何使用ext4
HACK #17 如何使用ext4 本节介绍ext4的编写和挂载方法.开发版ext4的使用方法.ext4是ext3的后续文件系统,从Linux 2.6.19开始使用.现在主要的发布版中多数都是采用ex ...
- JQ 确定与取消弹出框,选择确定执行Ajax
$(function () { $("#GetCoupon").click(function () { function del() { var msg = "请确定领取 ...
- mysql 集群 数据同步
mysql集群配置在网站负载均衡中是必不可少的: 首先说下我个人准备的负载均衡方式: 1.通过nginx方向代理来将服务器压力分散到各个服务器上: 2.每个服务器中代码逻辑一样: 3.通过使用redi ...