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. EasyUi组合条件分页查询

    1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...

  2. MongoDB之安装和基本使用(一)

    环境 ubuntu16.04 mongodb基本特点 MongoDB 是一个基于分布式 文件存储的NoSQL数据库;可以把MongoDB想象成一个大py字典. 模式自由 :可以把不同结构的文档存储在同 ...

  3. Linux端口占用

    1.netstat netstat -anp | grep 23232 Sample: [root@BICServer 0825]# netstat -anp | grep 23232 tcp 0 0 ...

  4. 123.Best Time to Buy and Sell Stock III---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目大意:与122题类似,只是这 ...

  5. Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070058 android-studio 3.0 from canary 5 to canary 6

    我升级android-studio到了3.0 canary 6打包编译安装出现如下错误: 07-11 13:00:39.523 8913-8913/dcpl.com.myapplication E/A ...

  6. Linux中涉及到环境变量的文件

    1. 系统级 (a) /etc/profile : 在用户登录操作系统时,定制用户环境的第一个文件,应用于登录的每一个用户 ==> 该文件一般调用/etc/bash.bashrc文件 (b)/e ...

  7. Nginx1.8.1 编译扩展https

    nginx无缝编译扩展https 本贴只限用于通过编译安装的nginx,如果用的是yum源安装请卸载后参见 http://www.cnblogs.com/rslai/p/7851220.html 安装 ...

  8. POJ 1392 Ouroboros Snake(数位欧拉)

    题目链接:http://poj.org/problem?id=1392 题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径. 解题思路:也跟HDU ...

  9. day4 作业计算器

    作业:计算器开发 (1)实现加减乘除及拓号优先级解析: (2)用户输入 1 - 2 * ( (60-30 +(-40/5) * (-9-2*5/-3 + 7 /3*99/4*2998 +10 * 56 ...

  10. bzoj 1100

    思路:好脑洞啊... 把边和角转化为字符串,然后用反串跑kmp... #include<bits/stdc++.h> #define LL long long #define fi fir ...