CodeForces 165C - Another Problem on Strings(思路)

题意:给定一个长度为 n (1 <= n <= 10^6 ) 的01串,求有多少连续子序列中包含 k 个 1。

从前往后记录到第 i 个元素存在了多少个 1,然后从前往后找个数 >= k 的元素,再找 - k 与 - k + 1 之间与多少个元素加入结果即可。

 

#include<cstdio>  
#include<cstring>  
#include<cctype>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<deque>  
#include<queue>  
#include<stack>  
#include<list>  
typedef long long ll;  
typedef unsigned long long llu;  
const int MAXN = 100 + 10;  
const int MAXT = 1000000 + 10;  
const int INF = 0x7f7f7f7f;  
const double pi = acos(-1.0);  
const double EPS = 1e-6;  
using namespace std;  
  
int n, k, num[MAXT];  
char s[MAXT];  
  
  
  
int main(){  
    scanf("%d%s", &k, s + 1);  
    n = strlen(s + 1);  
    num[0] = 0;  
    for(int i = 1; i <= n; ++i){  
        num[i] = num[i - 1];  
        if(s[i] == '1')  ++num[i];  
    }  
    num[n + 1] = num[n] + 1;  
    llu ans = 0;  
    for(int i = 1; i <= n; ++i)  
        if(num[i] >= k){  
            int c1 = lower_bound(num, num + i, num[i] - k) - num;  
            int c2 = upper_bound(num, num + i, num[i] - k) - num;  
            ans += llu(c2 - c1);  
        }  
    printf("%I64d\n", ans);  
    return 0;  
}  

 

转载于:https://www.cnblogs.com/tyty-TianTengtt/p/5995998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值