【To Understand !!! DP or 递归】LeetCode 87. Scramble String
LeetCode 87. Scramble String
参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439
[2]http://www.cnblogs.com/grandyang/p/4318500.html
Solution1:
递归
class Solution {
public:bool isScramble(string s1, string s2) {return isScramble(s1, 0, s1.size() - 1, s2, 0, s2.size() - 1);}private:bool isScramble(string s1, int a1, int b1, string s2, int a2, int b2) {if (b1 - a1 != b2 - a2)return false;if (a1 == b1)return s1[a1] == s2[a2];// 剪枝,如果子串中的字符不同,则必然不是Scramble String,不用再进行分割处理了int cnt[256] = {0};for(int i = a1; i <= b1; ++ i)++cnt[s1[i]];for(int i = a2; i <= b2; ++ i)--cnt[s2[i]];for(int i = 0; i < 256 ;++ i)if (cnt[i] != 0)return false;for (int i = a1; i < b1; ++ i) {if (isScramble(s1, a1, i, s2, a2, a2 + i - a1) && isScramble(s1, i + 1, b1, s2, b2 - (b1 - i - 1), b2))return true;if (isScramble(s1, a1, i, s2, b2 - (i - a1), b2) && isScramble(s1, i + 1, b1, s2, a2, a2 + b1 - i - 1))return true;}return false;}
};
Solution2:
DP
class Solution {
public:bool isScramble(string s1, string s2) {if (s1.size() != s2.size()) return false;if (s1 == s2) return true;int n = s1.size();vector<vector<vector<bool> > > dp (n, vector<vector<bool> >(n, vector<bool>(n + 1, false)));for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {dp[i][j][1] = s1[i] == s2[j];}}for (int len = 2; len <= n; ++len) {for (int i = 0; i <= n - len; ++i) {for (int j = 0; j <= n - len; ++j) {for (int k = 1; k < len; ++k) {if ((dp[i][j][k] && dp[i + k][j + k][len - k]) || (dp[i + k][j][len - k] && dp[i][j + len - k][k])) {dp[i][j][len] = true;}}}}}return dp[0][0][n];}
};
【To Understand !!! DP or 递归】LeetCode 87. Scramble String相关推荐
- 【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 Scramble String
LeetCode解题之Scramble String 原题 一个字符串能够拆分成两个都不为空的子字符串,而子字符串(长度大于等于二)也能够不断这样拆分下去.如今能够随意交换拆分出来两部分的位置来改变字 ...
- 【重点!DP】LeetCode 97. Interleaving String
LeetCode 97. Interleaving String 参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.c ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- LeetCode算法入门- String to Integer (atoi)-day7
LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...
- [LeetCode]844. Backspace String Compare 解题报告(C++)
[LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...
- LeetCode 87. 扰乱字符串(记忆化递归 / DP)
文章目录 1. 题目 2. 解题 2.1 记忆化递归 2.2 动态规划 1. 题目 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = &q ...
- leetcode 87. 扰乱字符串(dp)
使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行下述步骤: 在一个随机下标处将字符串分割成两个非空的子字符串.即,如 ...
- LeetCode 87. 扰乱字符串(递归)
题目描述 给定一个字符串 s1,我们可以把它递归地分割成两个非空子字符串,从而将其表示为二叉树. 下图是字符串 s1 = "great" 的一种可能的表示形式. great / g ...
最新文章
- java中日期怎么比较_如何比较Java中的日期?
- Saber2016安装包和安装详细安装步骤
- 更改WebBrowser控件的用户代理
- 8086标志寄存器介绍及作用(未完)
- row number函数_Hive排名函数ROW_NUMBER,RANK 和 DENSE_RANK的区别
- code forces 436 C. Bus
- dax powerbi 生成表函数_如何用Power BI对数据建模?
- 单片机 LCD1602液晶显示
- 被 YYUC $is_developing 害惨了.
- 云课堂智慧职教网页版登录入口_云课堂智慧职教网页版
- C语言pow函数返回值一直是0
- java 获得文本框文本_Java获取和删除Word文本框中的表格
- windows 系统R配置默认多核运算
- 无线WIFI系统如何解决短信认证功能?
- 自相关和相关的物理意义
- 2.4 设计评选优秀教师和学生的程序,其类结构如图 12.5 所示。当输入一系列教师 或学生的记录后,将优秀学生及教师的姓名列出来。 图 12.5 类结构 类 base char name[8]; 含
- SAP ME架构官网文档学习总结
- 解决:微信小程序按钮重复点击
- CMM数据分析系统三坐标
- Metasploit——辅助模块(Auxiliary)
热门文章
- Smart ORM v0.4.1开发计划
- 自动驾驶领域常用的数据集(检测,分割,定位)
- 树莓派模拟电路_基于树莓派的热电偶测量模块 MCC 134
- 熟练运用计算机进行数学教案,小学数学与信息技术整合教案
- python定时器毫秒_Python实现定时精度可调节的定时器
- python 输出颜色的设置
- python智能化推荐_windows python flask
- aic值检验 p值_23. 假设检验的时候为什么常写p lt; 0.05,而不写具体的p值?
- mysql中转换成字符串_如何在R中转换字符串的大小写?
- primefaces_Primefaces工具栏,工具栏组和工具提示