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.

题意:判断s2是否是s1的爬行字符串。

思路:如果s1和s2是scramble,那么必然存在一个在s1上的长度len1,将s1分成s11和s12两段同样有,s21和s22,那么要么s11和s21是scramble的并且s12和s22是scramble的;要么s11和s22是scramble的并且s12和s21是scramble的。如: rgeat 和 great 来说,rgeat 可分成 rg 和 eat 两段, great 可分成 gr 和 eat 两段,rg 和 gr 是scrambled的, eat 和 eat 当然是scrambled。参考了GrandyangEason Liu的博客。

代码如下:

 class Solution {
public:
bool isScramble(string s1, string s2)
{
if(s1.size() !=s2.size()) return false;
if(s1==s2) return true; string str1=s1,str2=s2;
sort(str1.begin(),str1.end());
sort(str2.begin(),str2.end());
if(str1 !=str2) return false; for(int i=;i<s1.size();++i)
{
string s11=s1.substr(,i);
string s12=s1.substr(i);
string s21=s2.substr(,i);
string s22=s2.substr(i); if(isScramble(s11,s21)&&isScramble(s12,s22))
return true;
else
{
s21=s2.substr(s1.size()-i);
s22=s2.substr(,s1.size()-i);
if(isScramble(s11,s21)&&isScramble(s12,s22))
return true;
} }
return false;
}
};

注:substr()函数的用法:str.substr(start,Length)从某一位置start开始,指定长度为length的子字符串,若,length为0或者为负数返回空串;若,没有指定该参数,则,从指定位置开始直到字符串的最后。

方法二:动态规划,参见Grandyang的博客,维护量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的.

代码如下:

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

[Leetcode] scramble string 乱串的更多相关文章

  1. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  3. [LeetCode] Scramble String 爬行字符串

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

  4. [leetcode]Scramble String @ Python

    原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...

  5. [Leetcode] Scramble String

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

  6. [LeetCode] Scramble String(树的问题最易用递归)

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

  7. [LeetCode] Scramble String 字符串 dp

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

  8. 【一天一道LeetCode】#87. Scramble String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

随机推荐

  1. mongodb的windows系统下安装

    先下载安装包,地址有下面两个,按需选择吧. https://www.mongodb.com/download-center/v2/community https://www.mongodb.org/d ...

  2. YII2.0 获取当前访问地址/IP信息

    假设我们当前页面的访问地址是:http://localhost/CMS/public/index.php?r=news&id=1 一. 1.获取当前域名:echo Yii::app()-> ...

  3. Spark-源码-Spark-Submit 任务提交

    Spark 版本:1.3 调用shell, spark-submit.sh args[] 首先是进入 org.apache.spark.deploy.SparkSubmit 类中调用他的 main() ...

  4. 最短路径算法 4.SPFA算法(1)

    今天所说的就是常用的解决最短路径问题最后一个算法,这个算法同样是求连通图中单源点到其他结点的最短路径,功能和Bellman-Ford算法大致相同,可以求有负权的边的图,但不能出现负回路.但是SPFA算 ...

  5. DAG上dp思想

    DAG上DP的思想 在下最近刷了几道DAG图上dp的题目.要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点.第二道是洛谷上的NOI导刊题 ...

  6. linux进程 生产者消费者

    #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> # ...

  7. xssbypass小记

    简单整理下bypass的一些点 标签外 如果是标签之外 又有htmlspecialchars函数的点 就别想了 在标签外同时能xss但是有长度限制 如果是储存型可以利用多个点 然后构造<scri ...

  8. php之apc浅探

    扩展编译: ./configure --enable-apc --with-php-config=/usr/local/php/bin/php-config --prefix=/usr/local/a ...

  9. Anytime项目开发记录2

    注册,登陆于密码找回.这是这次记录的主要内容. 首先,我们来看类图: 因为一直在改,所以与第二篇介绍项目框架时的图会有一些不一样. 代码都是非常简单的. 由于在注册和登陆这里,需要弹出一些对话框告诉用 ...

  10. ES5新增数组方法(4):every

    检查数组元素的每个元素是否符合条件. // 数组中的元素全部满足指定条件返回true let arr = [1, 3, 5, 7, 9]; console.log(arr.every((value, ...