Leetcode#87 Scramble String
两个字符串满足什么条件才称得上是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的更多相关文章
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 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 ...
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【LeetCode】87. Scramble String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...
随机推荐
- DNS笔记 DNS区域集成到 Active Directory
可以将 DNS 区域集成到 Active Directory 中以提供增强的容错功能和安全性.OpenDNS Google Public DNS往返时间 (RTT) 远程访问服务 (RAS)域名与 ...
- PHP多例模式
学习java就知道设计模式中有多例模式: 1.多例类可以有多个实例2.多例类必须能够自我创建并管理自己的实例,并向外界提供自己的实例. 大家都知道PHP单例模式,却很少说PHP多例模式,下面是在wik ...
- delphi 基础之四 delphi 组织结构
delphi 组织结构 在Delphi中,一个正在开发的应用程序可以被称作项目或者工程.一般地,一个项目主要由dpr(项目).pas(单元)和dfm(窗体)三种文件组成,另外还有一些附属文件,如res ...
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- 第四节:监视AppDomain
宿主应用程序可监视AppDomain消耗的资源.有的宿主根据这种信息判断一个AppDomain的内存或CPU消耗是否超过了应有的水准,并强制卸载一个AppDomain. 还可以利用监视来比较不同算法的 ...
- SynchronizationContext一篇
SynchronizationContext context; 最近写代码用到了这个,特别记录一下. 作用如下: // 摘要: // 提供在各种同步模型中传播同步上下文的基本功能. public cl ...
- python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html 1.__init__() 创建对 ...
- 关于消除超长的case列表的一个思路
最近项目里面涉及和低层通信的一个模块,因为需要定义通信协议,然后一大堆指令,定义一个枚举的话就能看到几十上百的数据项,匹配操作的时候,那么问题来了,尼玛,这么多的case看着就头晕,就有了一个想法:怎 ...
- GROUPING SETS、CUBE、ROLLUP
其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...
- 1084. Broken Keyboard (20)
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...