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

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

为了解决这个问题,我们需要将一个二进制字符串分割成三个非空子字符串,使得每个子字符串中包含相同数量的'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/

    你可能感兴趣的文章
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>