[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 ...
随机推荐
- mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install
- linux下的C语言开发(网络编程)
http://blog.csdn.net/feixiaoxing/article/details/7259675 [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing ...
- [C++11]_[0基础]_[左值引用声明和右值引用声明]
场景: 在 remove_reference 结构体中能看到右值引用的身影 &&, 那么这里的右值引用究竟有什么用呢? 常常也发现int& 和int&& 这两种 ...
- json解析神器 jsonpath的使用
转载:http://blog.csdn.net/qq_20641565/article/details/77162868 如果项目需求是从某些复杂的json里面取值进行计算,用jsonpath+IK( ...
- vue2.0 自定义 生成二维码(QRCode)组件
1.自定义 生成二维码组件 QRCode.vue <!-- 生成二维码 组件 --> <template> <canvas class="qrcode-canv ...
- dos alias/cname address
diego@localhost sdk/include/Poco/Net]# dig b.wpss.cn ; <<>> DiG - <<>> b.wps ...
- match excel test search replace 用法
1 test:测试string是否包含有匹配结果,包含返回true,不包含返回false. 2 reg.test(str) 3 <script type="text/javascrip ...
- [ssh新闻公布系统三]存储新闻
一.存储新闻dao方法 在NewsDao.java中新增存储新闻的saveOrupdate方法 public void saveOrupdate(News news){ getSession().sa ...
- mysql 执行sql文件的方法
http://philos.iteye.com/blog/162051 实战代码: #mysql导入mysql -um4n -p01D060A476642BA8335B832AC5B211F22 ...
- UVA 610 - Street Directions(割边)
UVA 610 - Street Directions option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...