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 ...
随机推荐
- php的register_shutdown_function函数详解
function shutdown() { $last_error = error_get_last(); if ($last_error) { error_log(print_r($last_err ...
- Laravel 5 基础(五)- 环境与配置
.env 文件是配置文件,包括数据库配置信息,查看 config->database.php ,connections 里面包含了所有数据库的配置,可以在 default 中选择要使用的数据库. ...
- delphi android 中 Toast 的实现(老外写的UNIT)
unit Android.JNI.Toast; // Java bridge class imported by hand by Brian Long (http://blong.com)interf ...
- POJ——3984
走迷宫问题,POJ上面的题 #include <stdio.h> #include <stdlib.h> #define SIZE 5 bool findpath = fals ...
- HelloWorld IL代码
.assembly extern mscorlib .assembly HelloWorld.class HelloWorld extends [mscorlib] System.Object { ...
- 【C#】使用C#将类序列化为XML
直接上代码: public static class XmlSerializer { public static void SaveToXml(string filePath, object sour ...
- jquery 小知识点
//计算checkbox有多少个被选中 $("input[name='user_apply']:checked").length)://可以查看 所有的name=user_appl ...
- Node.js 异步模式浅析
注:此文是node.js实战读后的总结. 在平常的脚本语言中都是同步进行的,比如php,服务器处理多个请求的方法就是并行这些脚本.多任务处理,多线程等等.但是这种处理方式也有一个问题:每一个进程或者线 ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- Android动画解析--XML
动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...