[Leetcode] scramble string 乱串
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。参考了Grandyang和Eason 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 乱串的更多相关文章
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] 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(树的问题最易用递归)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [LeetCode] Scramble String 字符串 dp
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
随机推荐
- React学习(2)—— 组件的运用和数据传递
React官方中文文档地址: https://doc.react-china.org/ 了解了组件之后,就需要理解“Props”和“State”的用法.首先来介绍State,State按照字面意 ...
- weui-switch开关控件,表单提交后如何取值
最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...
- 总结laravel假数据填充步骤
定义好模型 xxx.php 定义好数据生成的规则 database/factories/XxxlFactory.php 写入生成数据的代码,控制好生成的数据数目,对生成后的数据做出修改 databas ...
- hive 学习系列二(数据库的创建删除修改) 拿走,不谢。
database 相当于一个目录或者命名空间,用来更好地进行表的管理 在hdfs 的目录位置大致如下: [root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls ...
- 数据分析处理库Pandas——索引进阶
Series结构 筛选数据 指定值 备注:查找出指定数值的索引和数值. 逻辑运算 备注:查找出值大于2的数据. 复合索引 DataFrame结构 显示指定列 筛选显示 备注:值小于0的显示原值,否则显 ...
- python爬虫:爬取链家深圳全部二手房的详细信息
1.问题描述: 爬取链家深圳全部二手房的详细信息,并将爬取的数据存储到CSV文件中 2.思路分析: (1)目标网址:https://sz.lianjia.com/ershoufang/ (2)代码结构 ...
- python字符串编码
python默认编码 python 2.x默认的字符编码是ASCII,默认的文件编码也是ASCII. python 3.x默认的字符编码是unicode,默认的文件编码是utf-8. 中文乱码问题 无 ...
- QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ)
QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ) 2018年7月7日 20:29 拥塞:是指当前供给资源相对于正常转发处理需要资源的不足,从而导致服务质量下降的一种现象 拥塞管理概述: ...
- R语言绘图:直方图
使用ggplot2包绘制直方图 ######*****绘制直方图代码*****####### data1 <- data0[(data0[, 2] <= 500) & (data0 ...
- 利尔达NB-IOT的PSM和eDRX低功耗模式笔记
1. NB-IOT的技术优势,广覆盖,NB-IOT与GPRS和LTE相比较,最大链路预算提升了20dB,相当于提升了100倍,即使在地车车库.地下室.地下管道等普通无线网络信号难以到达的地方也容易覆盖 ...