[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)的博客中的讲解,下面的代码则与其代码稍有不同,个人认为更好理解一些。

 1 public class Solution {
 2     public boolean isScramble(String s1, String s2) {
 3         if (s1.length() != s2.length()) {
 4             return false;
 5         }
 6         if (s1.equals(s2)) {
 7             return true;
 8         }
 9         int len = s1.length();
10         boolean[][][] lenScramble = new boolean[len][len][len];
11         for (int i = 0; i < len; i++) {
12             for (int j = 0; j < len; j++) {
13                 lenScramble[0][i][j] = (s1.charAt(i) == s2.charAt(j));
14             }
15         }
16         for (int l = 2; l <= len; l++) {
17             int bound = len - l;
18             for (int i = 0; i <= bound; i++) {
19                 for (int j = 0; j <= bound; j++) {
20                     for (int k = 1; k < l; k++) {
21                         int l2 = l - k;
22                         if ((lenScramble[k - 1][i][j] && lenScramble[l2 - 1][i + k][j + k])
23                                 || (lenScramble[k - 1][i][j + l2] && lenScramble[l2 - 1][i + k][j])) {
24                             lenScramble[l - 1][i][j] = true;
25                             break;
26                         }
27                     }
28                 }
29             }
30         }
31         return lenScramble[len - 1][0][0];
32     }
33 }

这里的lenScramble[l][i][j]代表的是长度为l的s1中从i位置开始的substring和s2中从j位置开始的substring是否互为scramble string。状态转移倒是比较直接,就是枚举可能的切分点,然后分别考察上文说到的两种情况是否有至少一种成立,若成立则可立即把相应元素设为true然后break出循环。

整体说来这道题的思路其实比较常规,不过如果是第一次见到三维DP的话可能会在如何处理状态转移上拿捏不准,另外我在第一次做的时候想到了可能在状态转移时要考虑上文提到的两种情况,但是由于之前没有见过三维DP,始终觉得好像自己的思路是复杂度高到过不了OJ的。从这个角度讲,这题对于开阔见识题目种类还是蛮有意义的。

时间: 2024-10-10 17:57:27

[LeetCode] Scramble String -- 三维动态规划的范例的相关文章

天题系列: Scramble String -- 三维动态规划

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

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 substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble t

[LeetCode] Scramble String(树的问题最易用递归)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

LeetCode: Scramble String [87]

[题目] Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may cho

Leetcode:Scramble String 解题报告

Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string,

[LeetCode] Scramble String 爬行字符串

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

leetcode Scramble String 解题思路

https://leetcode.com/problems/scramble-string/ 首先递归,或说是递推是最容易想到的: F(s1(i,j), s2(i,j)) = F(s1(i,k),s2(i,k)) && F(s1(j-k,j),s2((j-k,j)) || F(s1(i,k),s2(j-k,j)) && F(s1(j-k,j),s2(i,k))  i<k<j; 图解: F(s1, s2) = F(s11,s21) && F(s12

[leetcode] Scramble String @python

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

[LeetCode] Scramble String 字符串 dp

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a