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

Below is one possible representation of s1 = "great":

    great
/ \
gr eat
/ \ / \
g r e at
/ \
a t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.分析:
这个问题是google的面试题。由于一个字符串有很多种二叉表示法,貌似很难判断两个字符串是否可以做这样的变换。
对付复杂问题的方法是从简单的特例来思考,从而找出规律。
先考察简单情况:
字符串长度为1:很明显,两个字符串必须完全相同才可以。
字符串长度为2:当s1="ab", s2只有"ab"或者"ba"才可以。
对于任意长度的字符串,我们可以把字符串s1分为a1,b1两个部分,s2分为a2,b2两个部分,满足((a1~a2) && (b1~b2))或者 ((a1~b2) && (a1~b2))

如此,我们找到了解决问题的思路。首先我们尝试用递归来写。

解法一(递归):
两个字符串的相似的必备条件是含有相同的字符集。简单的做法是把两个字符串的字符排序后,然后比较是否相同。
加上这个检查就可以大大的减少递归次数。
代码如下:

 class Solution {
public:
bool isScramble(string s1, string s2) {
int l1=s1.length();
int l2=s2.length();
if(l1!=l2) return false;
if(l1==) return s1==s2;
int A[]={};
for(int i=;i<l1;i++)
++A[s1[i]-'a'];
for(int i=;i<l2;i++)
--A[s2[i]-'a'];
for(int i=;i<;i++)
if(A[i]!=) return false;
bool res=false;
for(int i=;i<l1&&!res;i++){
res=isScramble(s1.substr(,i),s2.substr(,i))&&isScramble(s1.substr(i),s2.substr(i));
if(!res)
res=isScramble(s1.substr(,i),s2.substr(l1-i))&&isScramble(s1.substr(i),s2.substr(,l1-i));
}
return res;
}
};

解法二(动态规划):
当然,这道题也可以用动态规划Dynamic Programming,根据以往的经验来说,根字符串有关的题十有八九可以用DP来做,那么难点就在于如何找出递推公式。参见网友Code Ganker的博客,这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble。
有了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i][j][len]。判断这个是不是满足,其实我们首先是把当前s1[i...i+len-1]字符串劈一刀分成两部分,然后分两种情况:第一种是左边和s2[j...j+len-1]左边部分是不是scramble,以及右边和s2[j...j+len-1]右边部分是不是scramble;第二种情况是左边和s2[j...j+len-1]右边部分是不是scramble,以及右边和s2[j...j+len-1]左边部分是不是scramble。如果以上两种情况有一种成立,说明s1[i...i+len-1]和s2[j...j+len-1]是scramble的。而对于判断这些左右部分是不是scramble我们是有历史信息的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
上面说的是劈一刀的情况,对于s1[i...i+len-1]我们有len-1种劈法,在这些劈法中只要有一种成立,那么两个串就是scramble的。
总结起来递推式是res[i][j][len] = || (res[i][j][k]&&res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k]) 对于所有1<=k<len,也就是对于所有len-1种劈法的结果求或运算。因为信息都是计算过的,对于每种劈法只需要常量操作即可完成,因此求解递推式是需要O(len)(因为len-1种劈法)。
如此总时间复杂度因为是三维动态规划,需要三层循环,加上每一步需要线行时间求解递推式,所以是O(n^4)。虽然已经比较高了,但是至少不是指数量级的,动态规划还是有很大优势的,空间复杂度是O(n^3)。代码如下:

 // DP
class Solution {
public:
bool isScramble(string s1, string s2) {
if (s1.size() != s2.size()) return false;
if (s1 == s2) return true;
int n = s1.size();
vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + , false)));
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
dp[i][j][] = s1[i] == s2[j];
}
}
for (int len = ; len <= n; ++len) {
for (int i = ; i <= n - len; ++i) {
for (int j = ; j <= n - len; ++j) {
for (int k = ; k < len; ++k) {
if ((dp[i][j][k] && dp[i + k][j + k][len - k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k])) {
dp[i][j][len] = true;
}
}
}
}
}
return dp[][][n];
}
};

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] Scramble String 爬行字符串

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

  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. java基础知识回顾之---java String final类普通方法的应用之“两个字符串中最大相同的子串”

    /* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" *  * 思路: * 1,既然取得是最大子串,先看 ...

  6. java string截取两个字符串之间的值

    java string截取两个字符串之间的值 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  7. c#中从string数组转换到int数组及比较两个字符串相等

    string[] input = { "1", "2", "3", "4", "5", " ...

  8. C++入门经典-例6.21-比较string字符串,比较两个字符串

    1:使用“>”.“!=”.“>=”等比较运算符可以比较两个字符串的内容.比较的方法是将两个string字符串从头开始比较每一个字符,直到出现两者不一致.比较这两个不相同的字符的字面值,得出 ...

  9. [LintCode] Scramble String 爬行字符串

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

随机推荐

  1. 习题:Dual Matrices(思路题/分治)

    tyvj1764 描述一个N行M列的二维矩阵,矩阵的每个位置上是一个绝对值不超过1000的整数.你需要找到两个不相交的A*B的矩形,使得这两个矩形包含的元素之和尽量大.注:A*B的矩形指连续的A行.B ...

  2. HDU 3622 Bomb Game(二分+2-SAT)

    Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. bzoj1093【ZJOI2007】最大半联通子图

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1093 sol  :一开始理解错题意了QAQ,还莫名其妙写挂了QAQ,调了半天 首先显然一个强联 ...

  4. java 后台封装json数据学习总结(二)

    一.JSONArray的应用 从json数组中得到相应java数组,如果要获取java数组中的元素,只需要遍历该数组. /* * 从json数组中得到相应java数组 * JSONArray下的toA ...

  5. vue.js单文件组件中非父子组件的传值

    最近在研究vue.js,总体来说还算可以,但是在web开发群里有一些人问在单文件组件开发模式中非父子组件如何传值的问题,今天在这里讲讲,希望对大家有所帮助! 在官网api中的这段讲解很少,也很模糊:官 ...

  6. 使用state改变的jsx监听不到数据变化的问题

    当使用state来改变一个组件内部的虚拟dom的时候,该虚拟dom是无法监听到state数据的变化的,他只会绑定state改变dom当时的数据.

  7. python面试题目【转1】

    原文地址:https://www.usblog.cc/blog/post/justzhl/b5cc9a05c7d2 问题一:以下的代码的输出将是什么? 说出你的答案并解释. 1 2 3 4 5 6 7 ...

  8. BZOJ 3538 == 洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's

    P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题目描述 Farmer John has recently purchased a new car online, but ...

  9. wap开发杂项1

    原文发布时间为:2010-08-31 -- 来源于本人的百度文章 [由搬家工具导入] wap开发全程记忆[CLQ原创 持续更新] 1. 据说手机wap最好都是utf-8字符集,不过gb2312对移动手 ...

  10. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---33

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: