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分配数组真麻烦, 下标小心死了 |
|