87. Scramble String (String; DP)
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,那么必须两个字符串完全相同;
- 如果字符串长度为2,例如s1='ab',则s2='ab'或s2='ba'才行
- 如果字符串任意长度,那么可以把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)的更多相关文章
- [LeetCode] 87. Scramble String 搅乱字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 87. Scramble String
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- [leetcode]87. Scramble String字符串树形颠倒匹配
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 87. 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 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 87. Scramble String (Java)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 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 subs ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- SpringBoot 自定义线程池
本教程目录: 自定义线程池 配置spring默认的线程池 1. 自定义线程池 1.1 修改application.properties task.pool.corePoolSize=20 task.p ...
- 【转载】webstorm-前端javascript开发神器中文教程和技巧分享
webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载:百度网盘下载:http://pan.baidu. ...
- jenkins持续集成3
1.安装Pipeline插件,并初识 1.启动Jenkins,打开浏览器http://localhost:8080,系统管理,用户名:chenshanju/123456 2.系统管理-插件管理,安装p ...
- 在win7/WINDOWS SERVER 2008 R2上安装 vmware POWERcli 6.5
安装.NET Framework 4.6.2下载NDP462-KB3151800-x86-x64-AllOS-ENU.exe,安装安装PowerShell 4.0(5.0依赖4.0)下载Windows ...
- [UE4]C++静态局部变量
void testFunc() { ; // this only runs ONCE, even on // subsequent calls to testFunc()! cout << ...
- Notepad++ 删除空白行的方法(转)
Notepad++ 是我特别喜欢的一款编程工具.在安装后就可以轻松使用了.Notepad++ 上提供了很多方便的插件以实现更多的扩展,当然自身已经比较强大好用了.如果你遇到文本中间有大量的空白行的话, ...
- 第15章 高并发服务器编程(1)_非阻塞I/O模型
1. 高性能I/O (1)通常,recv函数没有数据可用时会阻塞等待.同样,当socket发送缓冲区没有足够多空间来发送消息时,函数send会阻塞. (2)当socket在非阻塞模式下,这些函数不会阻 ...
- MySQL 创建数据库的两种方法
使用 mysqladmin 创建数据库 使用普通用户,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用户拥有最高权限,可以使用 mysql mys ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- 报错:ORA-02264
创建表时报错ORA-02264:名称已被一个现有约束条件占用 查询约束名称“PK_DATASOUCE”,然后删除. SELECT a.* FROM user_constraints a where c ...