LeetCode: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.                                                                                本文地址

递归解法:s2[0...j]是否可以由s1[0...i]转换 isScramble(s2[0...j], s1[0...i]),可以分解成 i 个子问题(i 其实等于j,因为两个字符串长度不一样,肯定不能互相转换):

( isScramble(s2[0...k], s1[0...k]) &&  isScramble(s2[k+1...j], s1[k+1...i]) ) || ( isScramble(s2[0...k], s1[i-k...i]) &&  isScramble(s2[k+1...j], s1[0...i-k-1]) ),(k = 0,1,2 ... i-1,k相当于字符串的分割点)

只要一个子问题返回ture,那么就表示两个字符串可以转换。代码如下:

 1 class Solution {
 2 public:
 3     bool isScramble(string s1, string s2) {
 4         return isScrambleRecur(s1,s2);
 5     }
 6     bool isScrambleRecur(string &s1, string &s2)
 7     {
 8         string s1cop = s1, s2cop = s2;
 9         sort(s1cop.begin(), s1cop.end());
10         sort(s2cop.begin(), s2cop.end());
11         if(s1cop != s2cop)return false;//两个字符串所含字母不同
12         if(s1 == s2)return true;
13         
14         int len = s1.size();
15         for(int i = 1; i < len; i++)//分割位置
16         {
17             string s1left = s1.substr(0, i);
18             string s1right = s1.substr(i);
19             string s2left = s2.substr(0, i);
20             string s2right = s2.substr(i);
21             if(isScrambleRecur(s1left, s2left) && isScrambleRecur(s1right, s2right))
22                 return true;
23             s2left = s2.substr(0, len-i);
24             s2right = s2.substr(len-i);
25             if(isScrambleRecur(s1left, s2right) && isScrambleRecur(s1right, s2left))
26                 return true;
27         }
28         return false;
29     }
30 };

动态规划解法

递归解法有很多重复子问题,比如s2 = rgeat, s1 = great 当我们选择分割点为0时,要解决子问题 isScramble(reat, geat),再对该子问题选择分割点0时,要解决子问题 isScramble(eat,eat);而当我们第一步选择1作为分割点时,也要解决子问题 isScramble(eat,eat)。相同的子问题isScramble(eat,eat)就要解决2次。

动态规划用数组来保存子问题,设dp[k][i][j]表示s2从j开始长度为k的子串是否可以由s1从i开始长度为k的子串转换而成,那么动态规划方程如下

  1.    初始条件:dp[1][i][j] = (s1[i] == s2[j] ? true : false)
  2.    dp[k][i][j] = ( dp[divlen][i][j] && dp[k-divlen][i+divlen][j+divlen] )  ||  ( dp[divlen][i][j+k-divlen] && dp[k-divlen][i+divlen][j] ) (divlen = 1,2,3...k-1, 它表示子串分割点到子串起始端的距离) ,只要一个子问题返回真,就可以停止计算

代码如下:

 1 class Solution {
 2 public:
 3     bool isScramble(string s1, string s2) {
 4         //动态规划解法
 5         if(s1.size() != s2.size())return false;
 6         const int len = s1.size();
 7         //dp[k][i][j]表示s2从j开始长度为k的子串是否可以由s1从i开始长度为k的子串转换而成
 8         bool dp[len+1][len][len];
 9         //初始化长度为1的子串的dp值
10         for(int i = 0; i <= len-1; i++)
11             for(int j = 0; j <= len-1; j++)
12                 dp[1][i][j] = s1[i] == s2[j] ? true : false;
13         for(int k = 2; k <= len; k++)//子串的长度
14             for(int i = 0; i <= len-k; i++)//s1的起始位置
15                 for(int j = 0; j <= len-k; j++)//s2的起始位置
16                 {
17                     dp[k][i][j] = false;
18                     //divlen表示两个子串分割点到子串起始端的距离
19                     for(int divlen = 1; divlen < k && !dp[k][i][j]; divlen++)
20                         dp[k][i][j] = (dp[divlen][i][j] && dp[k-divlen][i+divlen][j+divlen])
21                             || (dp[divlen][i][j+k-divlen] && dp[k-divlen][i+divlen][j]);
22                 }
23         return dp[len][0][0];
24     }
25 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3452004.html

转载于:https://www.cnblogs.com/TenosDoIt/p/3452004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值