您的位置:首页 > 其它

LeetCode题目:Scramble String,三维动态规划

2015-02-26 01:46 537 查看
一开始拿到这个题的时候没什么想法,浆糊了之后立马百度之,才有了思路。

简单的说,就是s1和s2是scramble的话,那么必然存在一个在s1上的长度l1,将s1分成s11和s12两段,同样有s21和s22。

那么要么s11和s21是scramble的并且s12和s22是scramble的;

要么s11和s22是scramble的并且s12和s21是scramble的。

先用递归写了一个算法,但是大集合不过,然后用三维动态规划才搞定。

题目描述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 any non-leaf node and swap its two children.

For example, if we choose the node “gr” and swap its two children, it produces a scrambled string “rgeat”.
rgeat
/    \
rg    eat
/ \    /  \
r   g  e   at
/ \
a   t


We say that “rgeat” is a scrambled string of “great”.

Similarly, if we continue to swap the children of nodes “eat” and “at”, it produces a scrambled string “rgtae”.
rgtae
/    \
rg    tae
/ \    /  \
r   g  ta  e
/ \
t   a


We say that “rgtae” is a scrambled string of “great”.

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

代码:三维动态规划,48ms过大集合
bool isScrambleDP(string s1, string s2) {
if(s1.size() != s2.size()) return false;
if(s1.size() == 0) return true;
if(s1 == s2) return true;
bool ***iss = NULL;//iss[len][startIndexAtS1][startIndexAtS2]
iss = new bool**[s1.size()];
for(int len = 1;len <= s1.size(); ++len) {
//size at this level
int levelSize = s1.size() - len + 1;
int levelIndex = len - 1;
iss[levelIndex] = new bool*[levelSize];
for(int indexS1 = 0;indexS1 < levelSize; ++ indexS1) {
iss[levelIndex][indexS1] = new bool[levelSize];
for(int is2 = 0; is2 < levelSize; ++is2) {
if(len == 1) {
iss[levelIndex][indexS1][is2] = (s1[indexS1] == s2[is2]);
} else {
iss[levelIndex][indexS1][is2] = false;
for(int seglen1 = 1; seglen1 < len; ++seglen1) {
int seglen2 = len - seglen1;
int sli1 = seglen1 - 1;
int sli2 = seglen2 - 1;
if(iss[sli1][indexS1][is2] && iss[sli2][indexS1 + seglen1][is2 + seglen1]) {
iss[levelIndex][indexS1][is2] = true;
break;
}
if(iss[sli1][indexS1][is2 + seglen2] && iss[sli2][indexS1 + seglen1][is2]) {
iss[levelIndex][indexS1][is2] = true;
break;
}
}
}
}
}
}
return iss[s1.size() - 1][0][0];
}


递归代码,小集合12ms过,大集合过不了,因为时间复杂度是O(3n)
class Solution {
public:
bool isScramble(string s1, string s2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s1.size() != s2.size()) return false;
if(s1 == s2) return true;
for(int isep = 1; isep < s1.size(); ++ isep) {
//seporate s1 as [0,isep - 1],[isep, s1.size() - 1]
string seg11 = s1.substr(0,isep);
string seg12 = s1.substr(isep);
{//see if forward order is ok
string seg21 = s2.substr(0,isep);
string seg22 = s2.substr(isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true;
}
{//see if reverse order is ok
string seg21 = s2.substr(s2.size() - isep);
string seg22 = s2.substr(0,s2.size() - isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true;
}
}
return false;
}
};


Recursive code rewrite at 2013-2-5, 48ms pass large set
class Solution {
public:
bool isScramble(string s1, string s2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s1.size() != s2.size()) return false;
if(s1 == s2) return true;
string ts1 = s1,ts2 = s2;
sort(ts1.begin(), ts1.end());
sort(ts2.begin(), ts2.end());
if(ts1 != ts2) return false;
for(int isep = 1; isep < s1.size(); ++ isep) {
//seporate s1 as [0,isep - 1],[isep, s1.size() - 1]
string seg11 = s1.substr(0,isep);
string seg12 = s1.substr(isep);
{//see if forward order is ok
string seg21 = s2.substr(0,isep);
string seg22 = s2.substr(isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true;
}
{//see if reverse order is ok
string seg21 = s2.substr(s2.size() - isep);
string seg22 = s2.substr(0,s2.size() - isep);
if(isScramble(seg11,seg21) && isScramble(seg12,seg22)) return true;
}
}
return false;
}
};


发布于2012
年 10 月 23 日作者uniEagle分类C++Develop算法标签algorithmc++dpleetcode、Scramble
String、动态规划算法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: