LeetCode OJ 之 Scramble String

来源:互联网 发布:长春玛雅软件培训 编辑:程序博客网 时间:2024/06/06 17:19

题目:

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.

给两个字符串s1和s2,具有相同的长度,判断s2和s1是否是scrambled 字符串。

思路:

首先想到的是递归(即深搜),对两个string 进行分割,然后比较四对字符串。代码虽然简单,但是复杂度比较高。有两种加速策略,一种是剪枝,提前返回;一种是加缓存,缓存中间结果,即memorization(翻译为记忆化搜索)。
剪枝可以五花八门,要充分观察,充分利用信息,找到能让节点提前返回的条件。例如,判断两个字符串是否互为scamble,至少要求每个字符在两个字符串中出现的次数要相等,如果不相等则返回false。
加缓存,可以用数组或HashMap。本题维数较高,用HashMap,map 和unordered_map 均可。
既然可以用记忆化搜索,这题也一定可以用动规。设状态为f[n][i][j] ,表示长度为n,起
点为 s1[i] 和起点为s2[j] 两个字符串是否互为scramble,则状态转移方程为
f[n][i][j]} = (f[k][i][j] && f[n-k][i+k][j+k])

代码1:

class Solution {public:    bool isScramble(string s1, string s2)     {        int len = s1.size();        if(len != s2.size())            return false;        vector<vector<bool> > tmp(len,vector<bool>(len,false));        vector<vector<vector<bool> > > f(len+1,tmp);        for(int i = 0 ; i < len ; i++)        {            for(int j = 0 ; j < len ; j++)            {                f[1][i][j] = s1[i] == s2[j];            }        }        //f[n][i][j]} = (f[k][i][j] && f[n-k][i+k][j+k]) || (f[k][i][j+n-k] && f[n-k][i+k][j])        for(int n = 2 ; n <= len ; n++)        {            for(int i = 0 ; i + n <= len ; i++)            {                for(int j = 0 ; j + n <= len ; j++)                {                    for(int k = 1 ; k < n ; k++)                    {                        if((f[k][i][j] && f[n-k][i+k][j+k]) || (f[k][i][j+n-k] && f[n-k][i+k][j]))                        {                            f[n][i][j] = true;                            break;                        }                    }                }            }        }        return f[len][0][0];    }};

代码2:

class Solution {public:    bool isScramble(string s1, string s2)     {        int len = s1.size();        if(len != s2.size())            return false;        if(len == 1)            return s1 == s2;        string tmp1 = s1 , tmp2 = s2;        sort(tmp1.begin() , tmp1.end());        sort(tmp2.begin() , tmp2.end());        if(tmp1 != tmp2)            return false;        string s11 , s12 , s21 , s22;        bool result = false;        for(int i = 1 ; i < len && !result ; i++)        {            s11 = s1.substr(0 , i);            s12 = s1.substr(i);            s21 = s2.substr(0 , i);            s22 = s2.substr(i);            result = isScramble(s11,s21) && isScramble(s12,s22);            if(!result)            {                s21 = s2.substr(0 , len-i);                s22 = s2.substr(len-i);                result = isScramble(s11,s22) && isScramble(s12,s21);            }            if(result)                return true;        }        return false;    }};



0 0
原创粉丝点击