原题地址

两个字符串满足什么条件才称得上是scramble的呢?

如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系。

如果s1和s2的长度大于1,那么就对s1和s2进行分割,划分成两个子问题分别处理。

如何分割呢?当然不能任意分割。假设分割后s1变成了s11和s12,s2变成了s21和s22,那么只有2种分割方式:

1. s11.length = s21.length & s12.length = s22.length,如下图所示:

s1: ? ? ? ? ?
----- ---
s11 s12 s2: ? ? ? ? ?
----- ---
s21 s22

2. s11.length = s22.length & s12.length = s21.length,如下图所示:

s1: ? ? ? ? ?
----- ---
s11 s12 s2: ? ? ? ? ?
--- -----
s21 s22

经过分割后,我们可以得到长度相等的两组子串,正好是两个子问题。于是就可以递归分割下去了,直到两个串的长度长度为1停止分割。

令scramblep[i][j][k]表示s1[i..i+k]和s2[j..j+k]相互之间是否是scramble关系,即i是s1的子串起始位置,j是s2的子串起始位置,k是子串的长度。则有如下递推公式:

scramblep[i][j][k] = (scramblep[i][j][t] && scramblep[i+t][j+t][k-t]) || (scramblep[i][j+k-t][t] && scramblep[i+t][j][k-t]),其中0 < t < k

(正好对应上面说的两种分割方式)

初值为:scramblep[i][j][k] = s1[i..i+k] == s2[j..j+k],(如果两个子串相等,自然是scramble关系。这个初值包含了长度为1的情况)

代码:

 bool isScramble(string s1, string s2) {
if (s1.length() != s2.length()) return false;
if (s1.empty()) return true; int len = s1.length();
bool ***judge = new bool**[len];
for (int i = ; i < len; i++) {
judge[i] = new bool*[len];
for (int j = ; j < len; j++)
judge[i][j] = new bool[len + ];
} for (int k = ; k <= len; k++) {
for (int i = ; i + k <= len; i++) {
for (int j = ; j + k <= len; j++) {
judge[i][j][k] = s1.substr(i, k) == s2.substr(j, k);
for (int t = ; t < k && !judge[i][j][k]; t++) {
judge[i][j][k] |= judge[i][j + k - t][t] && judge[i + t][j][k - t];
judge[i][j][k] |= judge[i][j][t] && judge[i + t][j + t][k - t];
}
}
}
} return judge[][][len];
}

new出来的内存都没有delete,面试的时候别忘了啊。

Leetcode#87 Scramble String的更多相关文章

  1. [leetcode]87. Scramble String字符串树形颠倒匹配

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

  3. [LeetCode] 87. Scramble String 搅乱字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  4. [LeetCode] 87. Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  5. leetcode@ [87] Scramble String (Dynamic Programming)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  6. leetCode 87.Scramble String (拼凑字符串) 解题思路和方法

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  9. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

随机推荐

  1. WIN8 下 Hyper-V和Vmware Workstation

    1 管理员身份运行命令提示符 cmd bcdedit /copy {current} /d “Windows Without Hyper-V 2 记下 { } 中的代码 bcdedit /set {X ...

  2. PHP JS HTML ASP页面跳转代码 延时跳转代码

    1.PHP延时跳转代码 //跳转到浏览界面 header("Refresh:1;url=machine_list.php"); //不延时 <?php header(&quo ...

  3. 第九章 管理类型(In .net4.5) 之 继承机制

    1. 概述 本章包括 设计和实现接口.创建和使用基类 以及 使用.net类库提供的标准接口. 2. 主要内容 2.1 设计和实现接口 一个接口包含公共的方法.属性.事件.索引器.类和结构都可以实现接口 ...

  4. HelloWorld IL代码

    .assembly extern mscorlib .assembly HelloWorld.class HelloWorld extends [mscorlib] System.Object {   ...

  5. java reflect 初始学习 动态加载类

    首先要理解Class类: 在java 的反射中,Class.forName("com.lilin.Office") 使用类的全名,这样获取,不仅仅表示了类的类类型,同时还代表着类的 ...

  6. meteor 安装 android sdk慢的改进方法

    网上方法很多,最后总结一下比较靠谱的一个,到~/.meteor/android_bundle/ 目录下, 执行tools/android,手动下载 API 19 和 intel X86 Atom Sy ...

  7. Eclipse基金会

    昨天Eclipse基金会庆祝其成立十周年.2004年2月的新闻稿宣布该非盈利组织的正式成立,由包括开发者.消费者和插件提供商在内的各独立团体组成的董事会,为Eclipse的长期发展负责. 基金会成立时 ...

  8. Swift基础小结_2

    import Foundation // MARK: - ?和!的区别// ?代表可选类型,实质上是枚举类型,里面有None和Some两种类型,其实nil相当于OPtional.None,如果非nil ...

  9. Client–server model

    Client–server model From Wikipedia, the free encyclopedia The client–server model of computing ] Oft ...

  10. C++异常机制知识点

     在这里总结一下,C++中的异常机制,以及如何使用异常的知识点 C++中处理异常的过程是这样的:在执行程序发生异常,可以不在本函数中处理,而是抛出一个错误信息,把它传递给上一级的函数来解决,上一级解决 ...