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的更多相关文章

  1. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  2. [LintCode] Scramble String 爬行字符串

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

  3. 45. Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  4. 【LeetCode练习题】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

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

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

  6. [leetcode]87. Scramble String字符串树形颠倒匹配

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

  7. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  8. [LeetCode] Scramble String -- 三维动态规划的范例

    (Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...

  9. [leetcode] 87. Scramble String (Hard)

    题意: 判断两个字符串是否互为Scramble字符串,而互为Scramble字符串的定义: 字符串看作是父节点,从字符串某一处切开,生成的两个子串分别是父串的左右子树,再对切开生成的两个子串继续切开, ...

随机推荐

  1. .NET Framework 4安装失败

    #刚装系统遇到之前所遇到的问题.之前因为这个事情还被困扰了好一阵子.特此写出来分享给大家. 环境:WIN10 企业版 在使用一些需要较高.net版本的时候无法更新.你可以试一下.在服务里面开启再更新. ...

  2. webstrom 里面使用github

    1.输入github的账号和密码,点击登录 2.复制github的项目地址,现在clone就行了

  3. supervisor 的使用

    1.通过yum安装 supervisor: 2.supervisorctl 查看状态: 3.supervisor.d 下查看配置文件,修改命令和日志目录 4.tail -f /var/log/supe ...

  4. navigator.geolocation详解

    https://blog.csdn.net/qq_27626333/article/details/51815467 PositionOptions: JSON对象,监听设备位置信息参数 naviga ...

  5. leetcode 之Search in Rotated Sorted Array(四)

    描述 Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?    Would this aff ...

  6. 动手编写TCP服务器系列之一:日志文件

    前言 在几个月之前,笔者想自己实现一个性能比较良好的基于tcp的服务器.于是断断续续写了个把月,因为还需要找工,还有论文什么的.拖了这么久.现在开辟这样的一个博客,我想记录下自己的思路,也和大家分享自 ...

  7. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  8. windows 下 nginx 配置虚拟主机

    1. 在 nginx 的配置文件 nginx.conf 里面 引入虚拟主机配置文件,以后所有的虚拟主机配置文件都在写这个文件里 include       vhost.conf; (或者新建vhost ...

  9. scala学习笔记3

    一.条件表达式 在scala中if/else表达式有值,这个值就是跟在if或者else之后的表达式的值. scala> val x = 10 x: Int = 10 scala> val ...

  10. 洛谷P1319压缩技术 题解

    题目传送门 这道题是入门难度的题.特别水...QWQ...... #include<bits/stdc++.h> using namespace std; *],top; int main ...