原题地址

两个字符串满足什么条件才称得上是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. Cassandra 有限分页策略

    瀑布式分页 如果你的应用只需要瀑布式的分页,那么,Cassandra可以很好的支持,不过记得要指定好排序顺序. CLUSTERING ORDER BY (add_time DESC); 常见的分页,跳 ...

  2. Linux下mysql编译安装

    一.下载安装包 因为编译安装mysql需要cmake所以先编译安装cmake,这里都一起下载下来 下载cmake,网址https://cmake.org/download/ 下载mysql,网址htt ...

  3. mac里边配置android开发环境,intellij开发工具:

    1 在android的官网下载 android sdk的mac版 http://developer.android.com/sdk/index.html  选择mac的版本 下载后打开sdk-mana ...

  4. Internet Explorer 无法启用 JavaScript 怎么办?

    在 Internet Expllorer 8/9 中,有些同学在浏览网页时,收到提示:“需要启用 JavaScript …”,并且会发现网页上某些功能不能用了,比如点击网页里的按钮没反应等等. 怎么启 ...

  5. java8新特性笔记

    1.forEach(),遍历数据结构中的元素,括号内可以带一个闭包的方法 2.双冒号用法:forEach(this::doSchedule),如果运行环境是闭包,java允许使用双冒号的写法来直接调用 ...

  6. 了解Unix进程(2)

    1. 每个进程都有一个名字,crusher 可以得到: # process name puts $PROGRAM_NAME 10.downto(1) do | num | $PROGRAM_NAME ...

  7. MongoDB中通过MapReduce实现合计Sum功能及返回格式不一致问题分析

    建立下述测试数据,通过MapReduce统计每个班级学生数及成绩和. 代码如下: public string SumStudentScore() { var collection = _dataBas ...

  8. 修改后的SQL分页存储过程,利用2分法,支持排序

    /****** Object: StoredProcedure [dbo].[sys_Page_v3] Script Date: 08/13/2014 09:32:28 ******/ SET ANS ...

  9. c语言内存分配-malloc

    malloc 原型:(原来返回类型是char) extern void *malloc(unsigned int num_bytes); 头文件: #include <stdlib.h> ...

  10. spring 3 的 @value的使用

    Spring 3支持@value注解的方式获取properties文件中的配置值,大大简化了我们读取配置文件的代码.使用方式如下: 1.首先必须要配置properties文件的加载bean:在spri ...