[LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0)
作为一个小弱,这个题目是我第一次碰到三维的动态规划。在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情况如果我们从第一层切分点,或者说从较高层的切分点看的话,s1和s2切分点左边的子串所包含的字符的种类个数应该完全一致,同样右边也是完全一致;另一类是在较高层切分点进行的互换,这样我们如果在同层来考察s1和s2的话,会发现s1的切分点左侧的char和s2的切分点右侧的char种类和每种char的数目一致,s1的切分点右侧和s2的切分点左侧一致。因此我们需要考虑的状态转移除了涉及子串的长度,还涉及到s1和s2中的子串各自的起始位置,因此我们需要维护一个三维的DP数组来存储信息。这一点借鉴了code ganker(http://blog.csdn.net/linhuanmars/article/details/24506703)的博客中的讲解,下面的代码则与其代码稍有不同,个人认为更好理解一些。
public class Solution {
public boolean isScramble(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
if (s1.equals(s2)) {
return true;
}
int len = s1.length();
boolean[][][] lenScramble = new boolean[len][len][len];
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
lenScramble[0][i][j] = (s1.charAt(i) == s2.charAt(j));
}
}
for (int l = 2; l <= len; l++) {
int bound = len - l;
for (int i = 0; i <= bound; i++) {
for (int j = 0; j <= bound; j++) {
for (int k = 1; k < l; k++) {
int l2 = l - k;
if ((lenScramble[k - 1][i][j] && lenScramble[l2 - 1][i + k][j + k])
|| (lenScramble[k - 1][i][j + l2] && lenScramble[l2 - 1][i + k][j])) {
lenScramble[l - 1][i][j] = true;
break;
}
}
}
}
}
return lenScramble[len - 1][0][0];
}
}
这里的lenScramble[l][i][j]代表的是长度为l的s1中从i位置开始的substring和s2中从j位置开始的substring是否互为scramble string。状态转移倒是比较直接,就是枚举可能的切分点,然后分别考察上文说到的两种情况是否有至少一种成立,若成立则可立即把相应元素设为true然后break出循环。
整体说来这道题的思路其实比较常规,不过如果是第一次见到三维DP的话可能会在如何处理状态转移上拿捏不准,另外我在第一次做的时候想到了可能在状态转移时要考虑上文提到的两种情况,但是由于之前没有见过三维DP,始终觉得好像自己的思路是复杂度高到过不了OJ的。从这个角度讲,这题对于开阔见识题目种类还是蛮有意义的。
[LeetCode] Scramble String -- 三维动态规划的范例的更多相关文章
- [LeetCode] 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 ...
- google的面试题(三维动态规划的范例)——(87)Scramble String
转:http://www.cnblogs.com/easonliu/p/3696135.html 分析:这个问题是google的面试题.由于一个字符串有很多种二叉表示法,貌似很难判断两个字符串是否可以 ...
- [leetcode]Scramble String @ Python
原题地址:https://oj.leetcode.com/problems/scramble-string/ 题意: Given a string s1, we may represent it as ...
- [Leetcode] 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 字符串 dp
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] 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(树的问题最易用递归)
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 ...
随机推荐
- python matplotlib 绘图 和 dpi对应关系
dpi=1 600×400 dpi=2 1200×800 dpi=3 1800×1200 ........ dpi=21 (21×600)×(21×400) ---> 12600×8400 示例 ...
- poj 2932 Coneology (扫描线)
题意 平面上有N个两两不相交的圆,求全部最外层的,即不被其它圆包括的圆的个数并输出 思路 挑战程序竞赛P259页 代码 /* ************************************* ...
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
The await operator is used to wait for a promise to settle. It pauses the execution of an async func ...
- 阿里巴巴为什么主推HSF?比Dubbo有哪些优势?
作者:匿名用户链接:https://www.zhihu.com/question/39560697/answer/187538165来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- hdu 4432 数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...
- 浅谈PropertyChanged是如何被初始化的?
http://www.cnblogs.com/wpcockroach/p/3909081.html
- openCV2马拉松第18圈——坐标变换
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g 收入囊中 仿射变换 坐标映射 利用坐标映射做一些效果,例如以下 watermark/ ...
- POJ 2309 BST
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8565 Accepted: 5202 Description C ...
- hdu 1081 & poj 1050 To The Max(最大和的子矩阵)
转载请注明出处:http://blog.csdn.net/u012860063 Description Given a two-dimensional array of positive and ne ...
- Hadoop 服务器配置的副本数量 管不了客户端
副本数由客户端的参数dfs.replication决定(优先级: conf.set > 自定义配置文件 > jar包中的hdfs-default.xml)如果前两个都没有,就用最后一个ja ...