由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 做了一下scramble string
相关主题
LeetCode Scramble String 疑问Leetcode Scramble String简单解法
google scramble string O(n) 解法cc1501.3题,请帮忙测试下代码
facebook的面试题wildcard matching 大case runtime error
关于leetcode的Scramble String问题急问F家面试一题
leetcode-- scramble string问一个精华区里的题目
有人面试碰到过scramble string这个题吗?C++ 面试题
string scramble 的时间复杂度leetcode上wild match
scramble string被thank you的fb电面面经
相关话题的讨论汇总
话题: nlen1话题: szstr2话题: szstr1话题: prec话题: int
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
/*
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;
}
DP分配数组真麻烦, 下标小心死了
1 (共1页)
进入JobHunting版参与讨论
相关主题
被thank you的fb电面面经leetcode-- scramble string
G家电面,这回肯定挂了。附面经。有人面试碰到过scramble string这个题吗?
从Simplify Path问面试编程语言选择?string scramble 的时间复杂度
一道有关String的面试题scramble string
LeetCode Scramble String 疑问Leetcode Scramble String简单解法
google scramble string O(n) 解法cc1501.3题,请帮忙测试下代码
facebook的面试题wildcard matching 大case runtime error
关于leetcode的Scramble String问题急问F家面试一题
相关话题的讨论汇总
话题: nlen1话题: szstr2话题: szstr1话题: prec话题: int