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 isScramble(string s1, string s2) {
int l = s1.length(), i, j, k, t;
if( == l)
return true;
vector<vector<vector<bool>>> dp(l, vector<vector<bool>>(l, vector<bool>(l+, )));
//dp[i][j][k] means s1 starts from index i, s2 starts from index j, if the length k substring is the same
for(i = ; i < l; i++)
{
for(j = ; j < l; j++)
{
dp[i][j][] = (s1[i] == s2[j]);
}
}
for(k = ; k <= l; k++)
{
for(i = ; i < l && i+k <= l; i++)
{
for(j = ; j < l && j+k <= l; j++)
{
for(t = ; t < k; t++)
{
dp[i][j][k] = dp[i][j][t] && dp[i+t][j+t][k-t] || dp[i][j+k-t][t] && dp[i+t][j][k-t];
if(dp[i][j][k])
break;
}
}
}
}
return dp[][][l];
}
};

87. Scramble String *HARD* 动态规划的更多相关文章

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

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

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

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

  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. google的面试题(三维动态规划的范例)——(87)Scramble String

    转:http://www.cnblogs.com/easonliu/p/3696135.html 分析:这个问题是google的面试题.由于一个字符串有很多种二叉表示法,貌似很难判断两个字符串是否可以 ...

  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. 87. Scramble String (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 (拼凑字符串) 解题思路和方法

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

  9. [LeetCode] 87. Scramble String 爬行字符串

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

随机推荐

  1. 2018-2019-1 20189218《Linux内核原理与分析》第八周作业

    编译链接的过程 编译就是把文本形式源代码翻译为机器语言形式的目标文件过程. 链接是把目标文件.操作系统的启动代码和用到的库文件进行组织最终形成可执行代码的过程. 对于GCC来说,编译源代码并最终形成可 ...

  2. C++ 一串数字三位一节,用逗号隔开表示

    #include <iostream> #include <string> #include <sstream> using namespace std; stri ...

  3. 过滤Windows文件名中的非法字符

    转载:http://blog.csdn.net/infoworld/article/details/42033097 场景: 1. 通常生成文件时需要一个文件名,而生成文件名的方式可能是通过用户输入的 ...

  4. shell下如何删除文件的某一列

    答:cat file | awk '{$1=null;print $0}' (删除第一列)

  5. VS2017无法启动程序 操作在当前状态中是非法的

    工具--选项--调试--常规--启用asp.net的JavaScript调试(chrome和ie)去掉勾选

  6. 51NOD 1057 N的阶乘

    1057 N的阶乘 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题   输入N求N的阶乘的准确值.   Input 输入N(1 <= N <= 10000) ...

  7. rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)

    本文为博主原创,未经允许不得转载: 异常展示如下: org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao. ...

  8. 06_Flume_interceptor_时间戳+Host

    1.目标场景 2.flume agent配置文件 # define agent name, source/sink/channel name a1.sources = r1 a1.sinks = k1 ...

  9. HDU 6121 Build a tree(完全K叉树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...

  10. web.xml配置文件详细解读

    对于一个J2EE应用的开发者,或者叫java web后台的开发者来说.经常会和web.xml打交道,偶尔用到几个标签不知道啥意思.然后就度娘一下,久而久之虽然大概知道web.xml的基本使用方法,但是 ...