由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - LeetCode Scramble String 疑问
相关主题
做了一下scramble stringLeetCode 上的题目 AC Rate。
google scramble string O(n) 解法你们leetcode上scrambled string之流能10分钟写好?
关于leetcode的Scramble String问题Leetcode oj 的"scramble string"
leetcode-- scramble stringstring scramble 的时间复杂度
facebook的面试题scramble string
有人面试碰到过scramble string这个题吗?Leetcode Scramble String简单解法
急问F家面试一题cc1501.3题,请帮忙测试下代码
Leetcode problems' difficultywildcard matching 大case runtime error
相关话题的讨论汇总
话题: nlen1话题: szstr2话题: szstr1话题: prec话题: int
进入JobHunting版参与讨论
1 (共1页)
N****p
发帖数: 1691
1
为什么bdac 不是 abcd 的Scramble?是我题意理解错误么?
b******v
发帖数: 1493
2
题目多读几遍

【在 N****p 的大作中提到】
: 为什么bdac 不是 abcd 的Scramble?是我题意理解错误么?
C***U
发帖数: 2406
3
最近问这个题目的人不少啊。。。
leetcode上有解答么?

【在 N****p 的大作中提到】
: 为什么bdac 不是 abcd 的Scramble?是我题意理解错误么?
w****x
发帖数: 2483
4
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 != nLen2)
return false;
return _inner_can_scramble(szStr1, szStr2, nLen1);
}
bool _inner_can_scramble(const char szStr1[], const char szStr2[], int n)
{
assert(szStr1 && szStr2);
if (0 >= n || (n == 1 && szStr1[0] == szStr2[0]))
return true;
for (int i = 1; i < n; i++)
{
if ( _inner_can_scramble(szStr1, szStr2, i) &&
_inner_can_scramble(szStr1+i, szStr2+i, n-i))
return true;
if (_inner_can_scramble(szStr1, szStr2+n-i, i) &&
_inner_can_scramble(szStr1+i, szStr2, n-i))
return true;
}
return false;
}
//Obviously, previous recursion solution contains duplicated
//calculation, use DP to save the duplicated results
bool CanScrambleDP(const char* szStr1, const char* szStr2)
{
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 != nLen2)
return false;
//allocate
//pRec[i][j][k] means can szStr1(i ... i+k)
//be scrambled to szStr2(j ... j+k)
bool*** pRec = new bool**[nLen1];
for (int i = 0; i < nLen1; i++)
{
pRec[i] = new bool*[nLen1];
for (int j = 0; j < nLen1; j++)
{
pRec[i][j] = new bool[nLen1];
for (int k = 0; k < nLen1; k++)
pRec[i][j][k] = false;
}
}
for (int i = 0; i < nLen1; i++)
{
for (int j = 0; j < nLen1; j++)
pRec[i][j][0] = (szStr1[i] == szStr2[j]);
}
for (int l = 1; l < nLen1; l++)
{
for (int i = 0; i+l < nLen1; i++)
{
for (int j = 0; j+l < nLen1; j++)
{
bool bRes = false;
for (int k = i+1; k <= i+l; k++)
{
if ((pRec[i][j][k-i-1] && pRec[k][k][i+l-k])
|| (pRec[i][j+l+1-k+i][k-i-1] && pRec[k][j][i+l-k]))
{
pRec[i][j][l] = true;
break;
}
}
}
}
}
bool bRet = pRec[0][0][nLen1-1];
for (int i = 0; i < nLen1; i++)
{
for (int j = 0; j < nLen1; j++)
delete []pRec[i][j];
delete []pRec[i];
}
delete []pRec;
return bRet;
}
C***U
发帖数: 2406
5
你的recursion的方案 可能不用每个都跑一边
尝试用anagram这个函数
可以省去重复

【在 w****x 的大作中提到】
: /*
: scramble string,judge if one string can be scrambled to another one
: tiger
: / \
: ti ger
: / \ / \
: t i g er
: / \
: e r
: rotation is allowded

1 (共1页)
进入JobHunting版参与讨论
相关主题
wildcard matching 大case runtime errorfacebook的面试题
一道有关String的面试题有人面试碰到过scramble string这个题吗?
request solutions to 2 questions on leetcode急问F家面试一题
leetcode的scramble string的test cases是不是有问题?Leetcode problems' difficulty
做了一下scramble stringLeetCode 上的题目 AC Rate。
google scramble string O(n) 解法你们leetcode上scrambled string之流能10分钟写好?
关于leetcode的Scramble String问题Leetcode oj 的"scramble string"
leetcode-- scramble stringstring scramble 的时间复杂度
相关话题的讨论汇总
话题: nlen1话题: szstr2话题: szstr1话题: prec话题: int