[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 ...
随机推荐
- Unable to connect to the MKS : Failed to connect to server XXXXXX:903
Issue : 通过 vSphere 连接VM控制台的时候, 报错 Unable to connect to the MKS : Failed to connect to server X ...
- mySql 主从复制linux配置
总结: 主库(192.168.1.251): /etc/my.cnf.d/server.cnf [mysqld] log-bin=mysql-bin server-id=1 从库(192.168.1. ...
- Spin.js-CSS动画进度载入器
spin.js是一款很easy的CSS载入器,他是一款使用了VML(Vector Makeup Language)的CSS动画效果. spin.js的特性 他有着很强大的适应性.有着下面几个特性: 1 ...
- matplotlib简易新手教程及动画
做数据分析,首先是要熟悉和理解数据.所以掌握一个趁手的可视化工具是很重要的,否则对数据连个主要的感性认识都没有,怎样进行下一步的design 点击打开链接 还有一个非常棒的资料 Matplotlib ...
- PHP ORM操作MySQL数据库
ORM----Oriented Relationship Mapper,即用面向对象的方式来操作数据库.归根结底,还是对于SQL语句的封装. 首先,我们的数据库有如下一张表: 我们希望能够对这张表,利 ...
- session写入数据库
<?php class session { private static $handle = null; private static $ip = null; private static $l ...
- Ansible 汇总
不错的博客:https://www.cnblogs.com/EWWE/p/8146083.html 修改文件权限: 首先需要 vi /etc/ansible/hosts (用pip install, ...
- NAT和桥接的区别
NAT 桥接 NAT相当于是局域网中的局域网,把192.168.21.1当作外网ip,重新划分了一个网关(192.168.33.x) 网桥只是把网络桥接起来,还是原来的网关(192.168.21.x) ...
- Integrate NSX into Neutron
NSX is VMware's strategy for Software-defined networking, it was implemented purely in software, and ...
- Linux机器间ssh免密登录
前言 一台Linux机器通过ssh的方式连接别的机器或通过scp的方式传输文件,都需要输入密码. 为了解决每次输入密码的困扰,可采用添加密钥的方式实现. 实现过程 源服务器A,目标服务器B. 1.在源 ...