leetcode87. Scramble String
leetcode87. Scramble String
题意:
给定一个字符串s1,我们可以通过将它分解为两个非空子字符串来表示为二叉树。
思路:
递归解法
对于每对s1,s2. 在s1某处切一刀,s1分成left,right,然后在s2首部开始等长的地方切一刀,切成left,right.只要s1的left和s2的left,s1的right和s2的right同样也构成scramble string就可以了。 递归解法需要剪枝。
- 剪枝1:s1,s2不等长return
- 剪枝2:s1,s2字符不相同return
动态规划
这题动态规划解法时间不如递归的,但是这题用动态规划还是比较牛皮的。map[i][j][k]三维数组储存,i,j分别表示s1,s2开始的下标,k表示i,j开始后组成的长度。虽然说看到两个字符串比较感觉像动态规划。但是不好想呀。
参考博文
ac代码:
C++ (递归)
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(len1 == 1) return s1[0] == s2[0];
int cnt[256] = {0};
for(int i = 0; i < len1; i++) cnt[s1[i]]++;
for(int j = 0; j < len2; j++) cnt[s2[j]]--;
for(int i = 0; i < 256; i++)
if(cnt[i]) return false;
for(int i = 0; i < len1 - 1; i++)
{
if(isScramble(s1.substr(0,i + 1),s2.substr(0,i + 1)) && isScramble(s1.substr(i + 1),s2.substr(i + 1)) ||
isScramble(s1.substr(0,i + 1),s2.substr(len2 - 1 - i, i + 1)) && isScramble(s1.substr(i + 1),s2.substr(0,len2 - 1 - i)))
return true;
}
return false;
}
};
C++ (动态规划)
class Solution {
public:
bool isScramble(string s1, string s2) {
int len1 = s1.length();
int len2 = s2.length();
if(len1 != len2) return false;
if(!len1) return true;
vector<vector<vector<bool> > >map(len1, vector<vector<bool> >(len1, vector<bool>(len1, false)));
for(int j = 0; j < len1; j++)
for(int k = 0; k < len1; k++)
if(s1[j] == s2[k]) map[0][j][k] = true;
for(int i = 1; i < len1; i++)
{
for(int j = len1 - 1; j >= 0; j--)
{
for(int k = len1 - 1; k >= 0; k--)
{
if(k + i + 1 > len1 || j + i + 1 > len1) continue;
//map[i][j][k] = false;
for(int u = 0; u < i; u++)
{
if((map[u][j][k + i - u] && map[i - u - 1][j + u + 1][k]) ||
(map[u][j][k] && map[i - u - 1][j + u + 1][k + u + 1]))
{
map[i][j][k] = true;
break;
}
}
}
}
}
return map[len1 - 1][0][0];
}
};
leetcode87. 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 ...
- [LintCode] Scramble String 爬行字符串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 45. 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
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【一天一道LeetCode】#87. Scramble String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 解题报告
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] 87. Scramble String (Hard)
题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...
随机推荐
- sqlite3_get_table()
{ sqlite3 *db; char *errmsg=NULL; //用来存储错误信息字符串 char ret=0; int my_age=0; //类型根据要提取的数据类型而定 cha ...
- HttpURLConnection传json
private static String sendToWangTing(DataRow dataRow) throws IOException{ String ip = Configuration. ...
- [ python ] 各种推导式
各种推导式,主要使用示例演示用法 列表生成式 示例1:求0-9每个数的平方 li = [x*x for x in range(10)] print(li) # 执行结果: # [0, 1, 4, 9, ...
- Webcollector应用(二)
先吐槽一句哀家的人品,总在写好代码之后,网站默默的升级,没有一点点防备... 一.加代理 爬取一个网站的时候,爬了不到一半,IP被封了,整个内部局域网的所有电脑都不能访问网站了. public cla ...
- 浅谈Linux系统中如何查看进程 ——ps,pstree,top,w,全解
进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源.一般来说,Linux系统会在进程之间共享程序代码和系统函数库,所以在任何时刻内存中都只有代码的一份拷贝. 1,ps命令 作用:p ...
- 洛谷 P1469 找筷子 题解
题目传送门 先排序一遍,再一个一个判断是否有偶数个.注意for循环要i+=2. #include<bits/stdc++.h> using namespace std; ]; int ma ...
- 458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 得分(UVa1585)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- [实战]MVC5+EF6+MySql企业网盘实战(14)——思考
写在前面 从上面更新编辑文件夹,就一直在思考一个问题,之前编辑文件夹名称,只是逻辑上的修改,但是保存的物理文件或者文件夹名称并没有进行修改,这样就导致一个问题,就是在文件或者文件夹修改名称后就会找不到 ...
- Good Bye 2014 F - New Year Shopping
F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...