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.

思路:对付复杂问题的方法是从简单的特例来思考。简单情况:

  1. 如果字符串长度为1,那么必须两个字符串完全相同;
  2. 如果字符串长度为2,例如s1='ab',则s2='ab'或s2='ba'才行
  3. 如果字符串任意长度,那么可以把s1分为a1, b1两部分,s2分为a2,b2两部分。需要满足:((a1=a2)&&(b1=b2)) || ((a1=b2)&&(a2=b1)) =>可用递归
class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1 == s2) return true;
for(int isep = ; isep < s1.size(); ++ isep) { //traverse split pos
string seg11 = s1.substr(,isep);
string seg12 = s1.substr(isep); //see if a1=a2 &&b1=b2 is ok
string seg21 = s2.substr(,isep);
string seg22 = s2.substr(isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true; //see if a1=b2 &&a2=b1 is ok
seg21 = s2.substr(s2.size() - isep); //从后截取isep长度
seg22 = s2.substr(,s2.size() - isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true;
}
return false;
}
};

Result: Time Limit Exceeded

思路II: 动态规划。三维状态dp[i][j][k],前两维分别表示s1和s2的下标起始位置,k表示子串的长度。dp[i][j][k]=true表示s1(i, i+k-1)和s2(j, j+k-1)是scramble。

状态转移方程:if(dp[i][j][split] && dp[i+split][j+split][k-split] || dp[i][j+k-split][split] && dp[i+split][j][k-split]) dp[i][j][k]=true;

因为在状态转移方程中k又要分割成更小的值,所以必须已知小值,k从小到大遍历。

class Solution {
public:
bool isScramble(string s1, string s2) {
int len = s1.length();
if(len==) return true;
if(s1 == s2) return true; //初始状态
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 k = ; k <= len; k++) //从较短的子串开始分析,为了状态转方程
{
for(int s1Pointer = ; s1Pointer+k- < len; s1Pointer++)
{
for(int s2Pointer = ; s2Pointer+k- < len; s2Pointer++)
{
for(int split = ; split < k; split++) //levelSize长度的任意一种分割
{
if ((dp[s1Pointer][s2Pointer][split] && dp[s1Pointer+split][s2Pointer+split][k-split]) ||
(dp[s1Pointer][s2Pointer+k-split][split] && dp[s1Pointer+split][s2Pointer][k-split]))
{
dp[s1Pointer][s2Pointer][k] = true;
break;
};
}
}
}
}
return dp[][][len];
}
};

87. Scramble String (String; DP)的更多相关文章

  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. 87. Scramble String

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

  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. 87. Scramble String *HARD* 动态规划

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

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

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

  6. 87. Scramble String (Java)

    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 (Dynamic Programming)

    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 subs ...

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

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

随机推荐

  1. springMVC学习(2)

    一.使用注解的处理器映射器和适配器 1) 在这个下面的/org/springframework/web/servlet/DispatcherServlet.properties文件,从这个文件中加载处 ...

  2. windows下面安装easy_install和pip教程

    方便安装whl:安装完成后,可以使用pip install   xxx.whl 安装一个python轮子 python扩展库的路径:Python\Python36\Lib\site-packages\ ...

  3. unity3d动态加载dll的API以及限制

    Unity3D的坑系列:动态加载dll 一.使用限制 现在参与的项目是做MMO手游,目标平台是Android和iOS,iOS平台不能动态加载dll(什么原因找乔布斯去),可以直接忽略,而在Androi ...

  4. PHP使用mysqli扩展连接MySQL数据库

    这篇文章主要介绍了PHP使用mysqli扩展连接MySQL数据库,需要的朋友可以参考下 1.面向对象的使用方式 $db = new mysqli('localhost', 'root', '12345 ...

  5. 中国标准时间改为formatTime格式

    1.toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串): var time = new Date(); var formatTime = time.toLoc ...

  6. redis存储对象

      redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列 ...

  7. PHP提取多维数组指定一列的方法大全

    目录 1 array_column函数法 2 array_walk函数法 3 array_map函数法 4 foreach循环法 5 array_map变种 PHP中对多维数组特定列的提取,是个很常用 ...

  8. webserver有哪些

    http://blog.csdn.net/mfsh_1993/article/details/70245380 常用web服务器有Apache.Nginx.Lighttpd.Tomcat.IBM We ...

  9. spring security 配置xml 参考

    https://blog.csdn.net/zsq520520/article/details/77880491

  10. restfull 风格 参考 https://blog.csdn.net/jaryle/article/details/52141097

    https://www.cnblogs.com/xiaoxian1369/p/4332390.html :