博客
关于我
LeetCode 1573. 分割字符串的方案数(组合数学)
阅读量:216 次
发布时间:2019-03-01

本文共 2063 字,大约阅读时间需要 6 分钟。

为了解决这个问题,我们需要将一个二进制字符串分割成三个非空子字符串,使得每个子字符串中包含相同数量的'1'。我们将通过计算前缀和数组来高效地解决这个问题,并对结果取模。

方法思路

  • 前缀和数组:我们首先计算前缀和数组d,其中d[i]表示前i个字符中'1'的数量。
  • 特殊情况处理:如果字符串中没有'1',则只能有一种分割方法;如果'1'的数量不能被3整除,直接返回0。
  • 分割点寻找:找到左分割点l,使得前l个字符中有k个'1',其中k是总'1'数除以3。右分割点r则从右边寻找,使得从r到末尾的字符中有k个'1'。
  • 计算空隙数量:计算从左分割点右边开始到下一个'1'的连续'0'的数量,以及从右分割点左边开始到下一个'1'的连续'0'的数量。
  • 方案数计算:方案数为左右空隙数量的乘积,结果对10^9 + 7取模。
  • 解决代码

    #include 
    using namespace std;class Solution { static const int mod = 1e9 + 7; int d[maxm]; // 其中maxm = 1e5 +5 int numWays(string s) { int n = s.size(); if (n < 3) return 0; // 分割后的三个子串必须非空 fill(d, d, 0); d[0] = 0; for (int i = 1; i <= n; ++i) { d[i] = d[i-1] + (s[i-1] == '1' ? 1 : 0); } int tot = d[n]; if (tot == 0) { return (n-1) * (n-2) / 2 % mod; } if (tot % 3 != 0) { return 0; } int k = tot / 3; // 找左分割点l int l = 0; while (l <= n && d[l] != k) { l++; } if (l > n) return 0; // 没找到左分割点 // 找右分割点r,使得d[r-1] = d[n] -k int r = n; while (r > 0 && d[r-1] != (d[n] -k)) { r--; } if (r <= 0) return 0; // 没找到右分割点 // 计算cntl:从l+1开始到下一个'1'的0的数量 int cntl = 0; for (int i = l + 1; i <= n; ++i) { if (s[i-1] == '0') { cntl++; } else { break; } } // 计算cntr:从r-1开始到下一个'1'的0的数量 int cntr = 0; for (int i = r-2; i >= 0; --i) { if (s[i] == '0') { cntr++; } else { break; } } // 方案数是 (cntl +1) * (cntr +1) mod mod return ( (cntl + 1) * (cntr + 1) ) % mod; }};// 以下是示例代码的主函数,用于测试和使用int main() { string s = "10110"; int ans = Solution().numWays(s); cout << ans << endl; return 0;}

    代码解释

  • 前缀和数组:通过遍历字符串,计算每个前缀和数组元素,记录到当前位置为止的'1'数量。
  • 特殊情况处理:处理字符串中没有'1'或'1'数量不能被3整除的情况。
  • 分割点寻找:从左到右找到第一个满足条件的分割点,同样从右到左寻找另一个分割点。
  • 空隙计算:计算左右两侧的空隙数量,乘积即为分割方案数。
  • 结果输出:对结果取模后输出。
  • 这种方法确保了在O(n)时间复杂度内解决问题,适用于较长的字符串。

    转载地址:http://igkv.baihongyu.com/

    你可能感兴趣的文章
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>