Given s1s2s3, 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 解题思路的更多相关文章

  1. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  2. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  3. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  5. [leetcode]Interleaving String @ Python

    原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...

  6. [LeetCode] Maximum Gap 解题思路

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  8. [Leetcode] Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  9. [LeetCode] Distinct Subsequences 解题思路

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. Ubuntu不卸载ibus前提下安装搜狗输入法

    第一步 在命令行中输入以下行命令安装fictx框架 sudo apt-get install fcitx fcitx-config-gtk im-switch 第二步 去 http://pinyin. ...

  2. IE11的CSS兼容性问题

    最近测试给了我一大堆BUG,一瞅发现全是IE11的.吐槽一下这个浏览器真的比较特立独行.很多默认的样式跟别的浏览器不同,而且最明显的一点应该是padding左右内边距往往比别的浏览器大了一倍.但是当需 ...

  3. php怎么保留相除后几位小数:sprintf

    $n=0.1265489; echo sprintf("%.2f", $n); // 0.13

  4. PHP图片上传程序(完整版)

    从PHP100上搜刮来的,功能很强大.几乎考虑到了每个细节,与大家分享!~~~ <meta http-equiv="Content-Type" content="t ...

  5. 整理 iOS 9 适配中出现的坑(图文)

    作者:董铂然 授权本站转载. 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iO ...

  6. 动态网页技术---JSP

    JSP(全称JavaServer Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成HTML.XML或其他格式文档的Web网 ...

  7. Mybatis 学习

    1.  Mybatis 中 # 与 $ 符号的区别: a.    #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号. 如:order by #user_id#,如果传入的值是12,那么解 ...

  8. Uva10207 The Unreal Tournament

    题目链接戳这里 首先递归调用函数次数其实是可以预处理出来的,但是这里我们介绍一个更屌的做法. 设\(F(i,j)\)为求解\(P(i,j)\)所遍历的节点数目,则有\[F(0,j)=F(i,0)=0\ ...

  9. QLGame 2d Engine SpriteBatch类创建

    GitHub地址:https://github.com/wsgzxl/QLGame2dEngine 今天说五个问题: 1.前面说到的 颜色不对的问题,是因为FreeImage读取出来的数据格式与Ope ...

  10. [wikioi]石子归并

    http://wikioi.com/problem/1048/ 区间型动态规划.参考PPT:http://wenku.baidu.com/view/73c1ded5b9f3f90f76c61bc4.h ...