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 };
  • 相关阅读:
    C# 关于替换SQL中某个字段的部分内容
    c# sharepoint client object model 创建工作流历史记录
    c# sharepoint client object model 创建文档库
    c# sharepoint client object model 创建列表库
    C# Windows Server 2016 IIS的安装与配置
    C# 判断当前请求是GET还是POST
    SQL 无法执行脚本
    SharePoint 2013 配置 -- 基于表单的身份认证
    C# 浏览器URL地址殊字符转义编码
    C# Session 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3570518.html
Copyright © 2011-2022 走看看