[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 ... 
随机推荐
- iOS 改变Search Bar中Scope Button中文本的颜色和字体
			- (void)initSearchbar{ self.wineSearchBar.delegate = self; [self.wineSearchBar setScopeBarButtonTitl ... 
- JS里面的call, apply以及bind
			参考了这篇文章:http://www.tuicool.com/articles/EVF3Eb 给几个例子 function add(a,b) { alert(a+b); } function sub( ... 
- mac 下 virtualbox 配置全网通
			mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ... 
- MySQL常用SQL整理
			MySQL常用SQL整理 一.DDL #创建数据库 CREATE DATABASE IF NOT EXISTS product DEFAULT CHARSET utf8 COLLATE utf8_ge ... 
- jdk的动态代理源代码解析
			先看一下JDK的动态是怎么用的. package dynamic.proxy; import java.lang.reflect.InvocationHandler; import java.lang ... 
- mysql手动停止无响应查询方法
			http://www.chenweionline.cn/archives/61.htm 
- vs2012编译ffmpeg
			从官方网站down下来的ffmpeg没有pdb文件不方便调试,为此使用VS2012编译ffmpeg. 编译步骤: 一.安装MinGW,具体的安装方法上一篇文章已经有介绍这里不在赘述. 二.下载文件并放 ... 
- Fckeditor常见漏洞的挖掘与利用整理汇总
			查看编辑器版本号 FCKeditor/_whatsnew.html ------------------------------------------------------------- 2. V ... 
- Linux的基本使用
			检测某个地址是否可以通信:ping xx.xx.xx.xx 检测某个端口是否开启:telnet xx.xx.xx.xx port 端口:用来区别不同服务 常用命令: 创建一个目录 /data mkdi ... 
- Scrapy爬虫入门系列1  安装
			安装python2.7 参见CentOS升级python 2.6到2.7 安装pip 参见CentOS安装python setuptools and pip 依赖 https://docs.scra ... 
