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.

 class Solution {
public:
bool check(string s1, string s2) {
if(s1.length() != s2.length()) return false; string cp1 = s1, cp2 = s2;
sort(cp1.begin(), cp1.end());
sort(cp2.begin(), cp2.end());
for(int i=; i<cp1.length(); ++i) {
if(cp1[i] != cp2[i]) return false;
}
return true;
}
bool dfs(string s1, string s2) {
int m = s1.length(), n = s2.length();
if(!check(s1, s2)) return false;
if(m == ) {
if(s1 == s2) return true;
return false;
} string l, r, p, q;
for(int le = ; le < m; ++le) {
l = s1.substr(, le);
r = s1.substr(le, m - le);
p = s2.substr(, le);
q = s2.substr(le, m - le);
if(dfs(l, p) && dfs(r, q)) return true;
else {
p = s2.substr(m - le, le);
q = s2.substr(, m - le);
if(dfs(l, p) && dfs(r, q)) return true;
}
} return false;
} bool isScramble(string s1, string s2) {
int m = s1.length(), n = s2.length(); if(m != n) return false; return dfs(s1, s2);
}
};

leetcode@ [87] Scramble String (Dynamic Programming)的更多相关文章

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

    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 (Hard)

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

  5. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  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. uva 10608

    简单并查集  水水..... #include <cstdio> #include <cstring> #define maxn 30005 int fa[maxn],ans[ ...

  2. 看几道JQuery试题后总结(下篇)

    感谢圆友的提醒 昨天下午完成了9道试题中的前4道,之后好多园友存在些疑惑和建议,在这里我一并说一下吧.首先对于昨天第一题可能存在误导,在JQuery中并没有innerHTML这个属性,不过我们可以将J ...

  3. Centos之LAMP环境搭建

    原文:http://blog.sina.com.cn/s/blog_c02ed6590101d2sl.html 一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@ ...

  4. !! Scrum之 流程和术语

    !!Scrum之 流程和术语 http://www.cnblogs.com/zhoujg/archive/2009/07/15/1523680.html 以下将对一些术语进行简单介绍,以便大家现在开始 ...

  5. VS2012 Build相关

    最近写了一个小程序,用到了一些关于build方面的内容,google后,记录一下 1. VS工程下的bin和obj文件夹,bin文件夹下的debug和release文件夹,以及其中的文件 大家可以参考 ...

  6. 命令行下玩VC

    说明:(1)转载请注明出处:http://www.cnblogs.com/opangle/p/4298155.html (2)以下以VS2013为例,并假设VC安装路径为%VC_INSTALL_PAT ...

  7. uva11082 Matrix Decompressing

    网络流 首先算出每行每列的数的和. 每行的值减去c,每列的值减去R 然后每行和每列之间连边,容量为19. 这样一来,(i,j)的流量相当于(i,j)的值-1. 这样就避免了流量为0不对应答案的尴尬情况 ...

  8. 弹出框页面中使用jquery.validate验证控件

    弹出框页面中使用jquery.validate验证控件有几个问题需要解决: 1,弹出框的提交事件完成后如何关闭弹出框页面? 2,提交不成功如何返回当前页? 3,如果知道验证事件成功? 之前笔者都是JS ...

  9. php网页显示正方形图片缩略图

    需求是这样的:原始图片的大小是不定的,类似800*600.1000*756,现有一个页面要以正方形(60*60)显示这些图片,注意:图片只能在内存处理,不能缩小后保存到本地磁盘. 解决办法: html ...

  10. OA,ERP等源码一部分演示

    更多源码http://www.pssdss.com QQ:11851298 功能强大的JAVA开发的ERP源码http://cx050027.pssdss.com:8080/   用户名pssdss  ...