[LeetCode] Interleaving String 解题思路
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.
问题 : 给定三个字符串 s1, s2, s3, 求 s3 是不是由 s1 和 s2 交错组合而成。
感觉是一条比较典型的 DP 题目。
设 i, j, k 分别是 s1, s2, s3 待求解的当前下标。
- 当 s3[k] != s1[i] 且 s3[k] != s2[j], 则匹配失败
- 当 s3[k] 只和 s1[i] 相等,则 k++ 和 i++
- 当 s3[k] 只和 s2[j] 相等,则 k++ 和 j++
- 当 s3[k] == s1[i] 且 s3[k] == s2[j] ,则分别求 k++ 和 i++ ,以及 k++ 和 j++ ,两者取或运算
由于 s3 恰好有 s1 和 s2 交错行程,所以 s3 长度必然等于 s1 + s2 的长度,知道其中二者就能确定第三个数,这样只需要二维数组保存中间结果即可。
vector<vector<int>> vv;
bool interIleave(string s1, string s2, string s3, int p1, int p2, int p3){
// cout << s1 << "\n" << s2 << "\n" << s3 << "\n--------\n";
if (s1.size() == ) {
return (s2 == s3);
}
if (s2.size() == ) {
return (s1 == s3);
}
if (vv[p1][p3] != -) {
return vv[p1][p3];
}
if (s3[] != s1[] && s3[] != s2[]) {
vv[p1][p3] = false;
return false;
}
if (s3[] == s1[] && s3[] != s2[]) {
bool tmpb = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
vv[p1][p3] = tmpb;
return tmpb;
}
if (s3[] != s1[] && s3[] == s2[]) {
bool tmpb = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
vv[p1][p3] = tmpb;
return tmpb;
}
bool inter1 = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
bool inter2 = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
bool res = inter1 || inter2;
vv[p1][p3] = res;
return res;
}
bool isInterleave(string s1, string s2, string s3) {
vector<vector<int>> tmp(s1.size(), vector<int>(s3.size(), -));
vv = tmp;
if (s3.size() != s1.size()+s2.size()) {
return false;
}
if (s3.size() == ) {
return true;
}
bool res = interIleave(s1, s2, s3, , , );
return res;
}
[LeetCode] Interleaving String 解题思路的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- 实体框架(Entity Framework)简介
实体框架(Entity Framework)简介 简称EF,与ADO.NET关系 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R ...
- nginx 代理 proxy_pass设置
#img.test.com/img1 实际访问的路径是 http://127.0.0.1:123/a1 #img.test.com/img2 实际访问的路径是 http://127.0.0.1:123 ...
- 以前写过的ajax基础案例(王欢-huanhuan)
//load方法的使用 $('#loadBtn').click(function(){ //responseTxt 包含来自请求的结果数据 //statusTxt 包含请求的状态(" ...
- centos VM 识别U盘
在VM设置选项中,选择 USB Controller 选项,设置相关参数即可
- PHPUnit初试
先测试了一下加减,检查一下环境,又调用函数测试了服务器名. 源代码: class DemoController extends \Think\Controller { /** * @assert (5 ...
- 犯这个错误的肯定不止我一个 关于File
File.Create(string filePath)这种用法所有人都知道,这两天用到的时候却发现一个问题. 需要先判断文件是否存在,如果不存在则创建文件,然后向该文件写入数据,后续定时Append ...
- ASP.NET注意事项
1.服务器上bin目录下面的dll备份的时候,第一个点号之前的名字不能是一样的,否则会报错. 2.
- Sql server中DateDiff用法【转】
记录下来.每次使用都忘记.... DATEDIFF 函数 [日期和时间] 功能 返回两个日期之间的间隔. 语法 DATEDIFF ( date-part, date-expression-1, dat ...
- hdu 4762 && 2013 ACM/ICPC 长春网络赛解题报告
这次的答案是猜出来的,如果做得话应该是应该是一个几何概型的数学题: 答案就是:n/(m^(n-1)); 具体的证明过程: 1.首先枚举这M个点中的的两个端点,概率是:n*(n-1); 2.假设这个蛋糕 ...
- 【BZOJ 3476】 线段树===
59 懒惰的奶牛贝西所在的牧场,散落着 N 堆牧草,其中第 i 堆牧草在 ( Xi,Yi ) 的位置,数量有 Ai 个单位.贝西从家移动到某一堆牧草的时候,只能沿坐标轴朝正北.正东.正西.正南这四个 ...