zoukankan      html  css  js  c++  java
  • LeetCode

    Scramble String

    2014.2.27 00:04

    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.

    Solution:

      First I considered DFS as a solution, but gave up quickly because it was too time-consuming.

      Two strings s1 and s2 can be "scramble" only under either of the conditions below:

        1. there exists a partition for s1 and s2, such that s1.left and s2.left are scramble, s1.right and s2.right are scramble.

        2. there exists a partition for s1 and s2, such that s1.left and s2.right are scramble, s1.right and s2.left are scramble.

      With this you can solve the problem with dynamic programming.

      The DP array will require O(n^3) time and space, therefore the complexities are both O(n^3).

    Accepted code:

     1 // 2CE, 2RE, 1AC
     2 class Solution {
     3 public:
     4     bool isScramble(string s1, string s2) {
     5         int len1, len2;
     6         
     7         len1 = (int)s1.length();
     8         len2 = (int)s2.length();
     9         // their lengths must be at least equal
    10         if (len1 == 0 || len2 == 0 || len1 != len2) {
    11             return false;
    12         }
    13         
    14         // they must at least anagrams
    15         int c[256];
    16         int i, j, k, m;
    17         for (i = 0; i < 256; ++i) {
    18             c[i] = 0;
    19         }
    20         for (i = 0; i < len1; ++i) {
    21             ++c[s1[i]];
    22         }
    23         for (i = 0; i < len2; ++i) {
    24             --c[s2[i]];
    25         }
    26         for (i = 0; i < 256; ++i) {
    27             if (c[i] != 0) {
    28                 return false;
    29             }
    30         }
    31         
    32         int n = len1;
    33         int ***dp;
    34         dp = new int**[n];
    35         dp[0] =  new int*[n * n];
    36         for (i = 1; i < n; ++i) {
    37             dp[i] = &dp[0][0] + i * n;
    38         }
    39         dp[0][0] = new int[n * n * n];
    40         for (i = 1; i < n * n; ++i) {
    41             dp[i / n][i % n] = &dp[0][0][0] + i * n;
    42         }
    43         
    44         for (i = 0; i < n; ++i) {
    45             for (j = 0; j < n; ++j) {
    46                 for (k = 0; k < n; ++k) {
    47                     dp[i][j][k] = 0;
    48                 }
    49             }
    50         }
    51         
    52         for (i = 0; i < n; ++i) {
    53             for (j = 0; j < n; ++j) {
    54                 if (s1[i] == s2[j]) {
    55                     dp[0][i][j] = 1;
    56                 }
    57             }
    58         }
    59         for (i = 1; i < n; ++i) {
    60             for (j = 0; j + i < n; ++j) {
    61                 for (k = 0; k + i < n; ++k) {
    62                     for (m = 0; m < i; ++m) {
    63                         dp[i][j][k] = (dp[m][j][k] && dp[i - m - 1][j + m + 1][k + m + 1]) || 
    64                                       (dp[m][j][k + i - m] && dp[i - m - 1][j + m + 1][k]);
    65                         if (dp[i][j][k]) {
    66                             break;
    67                         }
    68                     }
    69                 }
    70             }
    71         }
    72         int result = dp[n - 1][0][0];
    73         
    74         delete[] dp[0][0];
    75         dp[0][0] = nullptr;
    76         delete[] dp[0];
    77         dp[0] = nullptr;
    78         delete[] dp;
    79         dp = nullptr;
    80         
    81         return result == 1;
    82     }
    83 };
  • 相关阅读:
    第三十七节 log日志模块
    第三十六节 更新备注信息
    第三十五节 取消关注的股票
    第三十四节 路由添加正则功能以及添加关注功能
    第三十三节 通过带有参数的装饰器完成路由功能
    第三十二节 带有参数的装饰器
    Web_CSS
    Web_HTML
    Python操作MySQL
    MySQL_索引原理
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3570518.html
Copyright © 2011-2022 走看看