zoukankan      html  css  js  c++  java
  • 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.

      该题解析参考自一博文和另一博文

      这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble,而答案应该就是res[0][0][s1.size()]
      有了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i][j][len]。判断这个是不是满足,其实我们首先是把当前s1[i...i+len-1]字符串劈一刀分成两部分,然后分两种情况:第一种是左边和s2[j...j+len-1]左边部分是不是scramble,以及右边和s2[j...j+len-1]右边部分是不是scramble;第二种情况是左边和s2[j...j+len-1]右边部分是不是scramble,以及右边和s2[j...j+len-1]左边部分是不是scramble。如果以上两种情况有一种成立,说明s1[i...i+len-1]和s2[j...j+len-1]是scramble的。而对于判断这些左右部分是不是scramble我们是有历史信息的,因为长度小于n的所有情况我们都在前面求解过了(也就是长度是最外层循环)。
    上面说的是劈一刀的情况,对于s1[i...i+len-1]我们有len-1种劈法,在这些劈法中只要有一种成立,那么两个串就是scramble的。
      总结起来递推式是res[i][j][len] = || (res[i][j][k]&&res[i+k][j+k][len-k] || res[i][j+len-k][k]&&res[i+k][j][len-k]) 对于所有1<=k<len,也就是对于所有len-1种劈法的结果求或运算。因为信息都是计算过的,对于每种劈法只需要常量操作即可完成,因此求解递推式是需要O(len)(因为len-1种劈法)。

      程序如下:

     1 class Solution {
     2 public:
     3     bool isScramble(string s1, string s2) {
     4         int szS1 = s1.size();
     5         int szS2 = s2.size();
     6         if(szS1 != szS2)
     7             return false;
     8         if(szS1 == 0 && szS2 == 0)
     9             return true;
    10         
    11         vector<vector<vector<bool> > > dp(szS1, vector<vector<bool> >(szS2, vector<bool>(szS1 + 1, false)));
    12         for(int i = 0; i < szS1; i++)
    13             for(int j = 0; j < szS2; j++)
    14                 dp[i][j][1] = s1[i] == s2[j];
    15         
    16         for(int len = 1; len < szS1 + 1; len++)
    17             for(int i = 0; i < szS1 - len + 1; i++)
    18                 for(int j = 0; j < szS2 - len + 1; j++)
    19                     for(int k = 1; k < len; k++)
    20                         if(dp[i][j][k] && dp[i+k][j+k][len-k] || dp[i][j+len-k][k] && dp[i+k][j][len-k]) // 前前后后,前后后前
    21                         {
    22                             dp[i][j][len] = true;
    23                             break;
    24                         }
    25         
    26         return dp[0][0][szS1];
    27     }
    28 };
  • 相关阅读:
    小知识!
    命令级的python静态资源服务。
    自定义滚动条样式-transition无效
    css:a:visited限制
    react16 渲染流程
    virtual-dom
    用video标签流式加载
    golang 代码笔记
    position:fixed not work?
    go/node/python 多进程与多核cpu
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4580754.html
Copyright © 2011-2022 走看看