leetcode[86] Scramble String
将一个单词按照这种方式分:
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".
这样就说他们是scrambled string。现在给你两个字符串怎么判断是否属于scrambled string呢
思路:
想了n久,木有什么好的idea。
然后学习了justdoit兄的博客,理解了后自己也跟着写了个递归的。
简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。
那么要么s11和s21是scramble的并且s12和s22是scramble的;
要么s11和s22是scramble的并且s12和s21是scramble的。
判断剪枝是必要的,否则过不了大数据集合。
class Solution {
public:
bool subScramble(string s1, string s2)
{
if (s1 == s2) return true;
int len = s1.size();
string sort_s1 = s1, sort_s2 = s2;
sort(sort_s1.begin(), sort_s1.end());
sort(sort_s2.begin(), sort_s2.end());
if (sort_s1 != sort_s2) return false; //如果字母不相等直接返回错误
for (int i = ; i < len; ++i)
{
string s1_left = s1.substr(,i);
string s1_right = s1.substr(i);
string s2_left = s2.substr(,i);
string s2_right = s2.substr(i);
if (subScramble(s1_left, s2_left) && subScramble(s1_right, s2_right))
return true;
s2_left = s2.substr(, len - i);
s2_right = s2.substr(len - i);
if (subScramble(s1_left, s2_right) && subScramble(s1_right, s2_left))
return true;
}
return false;
}
bool isScramble(string s1, string s2)
{
return subScramble(s1, s2);
}
};
如果有更好的方法,我们一般是不用递归的,因为递归往往复杂以致难以掌控。继续学习,发现果然有动态规划的方法。凭空想,确实难以想象。但看见之后又恍然明了。
本题递归复杂度是非多项式的。具体多少您探讨一下。动态规划的复杂度应该是O(n^4),连动态规划都这个复杂度了,可见此题之可怕啊。
三维动态规划:
dp[k][i][j]: s1的第i个开始长度为k的串和s2的第j个开始长度为k的串是否scrambled,是的话true,否则存false;
同递归核心思想,dp[k][i][j]应该可以从1到k-1长度的分割来求解,一旦有一个为true,那么dp[k][i][j]就为true并跳出。
即:
dp[k][i][j] = (dp[div][i][j] && dp[k-div][i+div][j+div] || dp[div][i][j+k-div] && dp[k-div][i+div][j]);
div从1到k-1,分两种情况,即s1的从i开始的div个和s2从j开始的div个scrambled并且从i+div开始的k-div个都匹配那么true,
同理,如果s1的i开始的div个和s2的后div个匹配以及剩下另外两部分也都匹配,那么true。直到找到true位置。如下代码,找到true后,for中的判断!dp[k][i][j]不成立就跳出了。
class Solution {
public:
// 动态规划 dp[k][i][j]存s1从第i个开始长度为k的串和s2从j开始长度为k的串是否scrambled
bool isScramble(string s1, string s2)
{
if (s1.size() != s2.size()) return false;
int len = s1.size();
bool dp[len+][len][len];
// initialization
for (int i = ; i < len; ++i)
for (int j = ; j < len; ++j)
dp[][i][j] = s1[i] == s2[j];
// dp
for (int k = ; k < len + ; ++k)
for (int i = ; i < len; ++i)
for (int j = ; j < len; ++j)
{
// initialization
dp[k][i][j] = false;
// once dp[k][i][j] is true, jump out
for (int div = ; div < k && !dp[k][i][j]; ++div)
dp[k][i][j] = (dp[div][i][j] && dp[k-div][i+div][j+div] || dp[div][i][j+k-div] && dp[k-div][i+div][j]);
}
return dp[len][][];
}
};
以下三点值得注意:
1. 此题递归只要主要判断排序后子串是否相等,不等直接剪枝,减少计算量。
2. 两中方法都用到的核心是把两个字符串都分成两部分,如果对应匹配或者交叉匹配满足,则true。
3. 就是substr的用法,如何准确判断子串。
leetcode[86] Scramble String的更多相关文章
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] 87. Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Leetcode#87 Scramble String
原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...
- 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 ...
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
随机推荐
- uva 592 Island of Logic (收索)
Island of Logic The Island of Logic has three kinds of inhabitants: divine beings that always tel ...
- POJ2299 Ultra-QuickSort 【树阵】+【hash】
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39529 Accepted: 14250 ...
- JAVA连接ACCESS、MYSQL、SQLSEVER、ORACLE数据库
. 概要 1.1 JDBC概念 JDBC(Java Database Connectivity)是Java语言为了支持SQL功能而提供的与数据库连接的用户的接口.JDBC中包含了一组由(Java)语言 ...
- 考试easy该,学习如何做?
我的两个学生(场和任)都讲了他们周末參加的一个认证考试不考大题考小题的事情.由感而发: 话说不用大题考,大概是不敢用大题考. 老师的教.和学生的学中.存在的一些问题得不到解决,整体讲,学生的学习效果没 ...
- git 如何让单个文件回退到指定的版本
1.进入到文件所在文件目录,或者能找到文件的路径查看文件的修改记录 1 $ git log MainActivity.java 结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 十天学Linux内核之第一天---内核探索工具类
原文:十天学Linux内核之第一天---内核探索工具类 寒假闲下来了,可以尽情的做自己喜欢的事情,专心待在实验室里燥起来了,因为大二的时候接触过Linux,只是关于内核方面确实是不好懂,所以十天的时间 ...
- PS多形式的部分之间复制“笨办法”
PS剪切页面,有时候你可能会遇到这样的情况:设计改进,但是,我们要具有相同的切片. 在此假设,可以直接用于切割片.我们可以节省大量的时间,又分为片. 但是,人们一般不会在你的上跨片设计PSD在变化,但 ...
- Android设计模式(五岁以下儿童)--简单工厂模式
1.面试的时候问这个问题: 在ListView 的item小程序.很多不同的显示风格.或者是,为了更好地维护,不同的样式,应该怎么做? 我一下就想到的是工厂的模式,利用project,编写ViewFa ...
- 基于GruntJS前端性能优化
在本文中,如何使用GruntJS为了使治疗简单的前端性能优化自己主动,我写了一个完整的样本放在Github上.能够參考一下.关于Yahoo的前端优化规则请參考:Best Practices for S ...
- Hadoop之——CentOS构造ssh否password登录注意事项
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46388809 前提配置:使用root登录改动配置文件:/etc/ssh/sshd_ ...