原题地址

两个字符串满足什么条件才称得上是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. centos下安装php环境

    centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...

  2. Python自然语言工具包(NLTK)入门

    在本期文章中,小生向您介绍了自然语言工具包(Natural Language Toolkit),它是一个将学术语言技术应用于文本数据集的 Python 库.称为“文本处理”的程序设计是其基本功能:更深 ...

  3. Oracle 查看相关优化器参数

    select x.ksppinm name, y.ksppstvl value, y.ksppstdf isdefault, decode(bitand(y.ksppstvf, 7), 1, 'MOD ...

  4. 14.python中的集合

    什么是集合?正如其字面的意思,一堆东西集中合并到一起.乍一听貌似和容器没什么差别,嗯,好吧,集合也算是一种容器. 在学习这个容器有什么不同之前,先看看集合是如何创建的: a = set() #可变集合 ...

  5. IO - 同步,异步,阻塞,非阻塞 (转)

    转自:http://blog.csdn.net/historyasamirror/article/details/5778378 向大牛学习,言归正传.同步(synchronous) IO和异步(as ...

  6. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  7. hdu 5427 A problem of sorting

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Description There are many ...

  8. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统 :1.技术简介之Mina连接

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Apache MINA(Multipurpose Infrastructure for Network Applic ...

  9. android开发分辨率问题解决方案

    dpi是什么呢?dpi是“dot per inch”的缩写,每英寸像素数.四种密度分类: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extr ...

  10. [转]linux中强大的screen命令

    [转]linux中强大的screen命令 http://pythonorg.diandian.com/post/2012-01-05/40027464147 今天用SCREEN用点生了,有几个功能不知 ...