1415: 小ho的01串 [字符串]

點擊打開鏈接

1415: 小ho的01串 [字符串]


題目描述

有一個由01組成的字符串,它好長呀--------一望無際

恩,說正題,小ho的數學不太好,雖然是學計算機的但是看見01也是很頭疼的,

現在他的老師想讓他計算出來包含K1的子串有多少個-----呀,頭要炸了!!!

小ho知道你的數學棒棒噠,所以來找你幫忙了。

輸入

第一行是一個數K

第二行是一個字符串str

0 < |str| ≤ 106

輸出

一個數S,代表了滿足條件的個數。

樣例輸入

2
101010

樣例輸出

6

57
1
#include<iostream>
2
#include<cstdio>
3
#include<cstdlib>
4
#include<cstring>
5
#include<algorithm>
6
#include<queue>
7
#include<list>
8
#include<cmath>
9
#include<vector>
10
using namespace std;
11
const int maxn=1000010;
12
char str[maxn];
13
long long num[maxn];
14
int main()
15
{
16
//  freopen("in1.txt","r",stdin);   //查BUG
17
//  freopen("out1.txt","w",stdout);
18
    long long ans=0,k,cnt=1;
19
    scanf("%lld%s",&k,str);
20
    long long l=1,r,len=strlen(str),temp=0;num[0]=-1;
21
    if(k==0)
22
    {
23
        l=0;
24
        for(long long i=0;i<len;++i)
25
        {
26
            if(str[i]=='1')
27
            {
28
                r=i-l;l=i+1;
29
                ans=ans+r*(r+1)/2;
30
                //cout<<ans<<endl;
31
            }
32
        }
33
        r=(len-l);
34
        ans=ans+r*(r+1)/2;
35
        cout<<ans<<endl;
36
        return 0;
37
    }
38
    for(long long i=0;i<len;++i)
39
    {
40
        if(str[i]=='1')
41
        {
42
            temp++;num[cnt++]=i;
43
        }
44
        if(temp==k+1)
45
        {
46
            ans=ans+(num[l]-num[l-1])*(num[cnt-1]-num[cnt-2]);l++;temp--;
47
            //cout<<num[l]-num[l-1]<<endl;
48
        }
49
    }
50
    if(temp==k)
51
    {
52
        ans=ans+(num[l]-num[l-1])*(len-num[cnt-1]);
53
    }
54
    cout<<ans<<endl;
55
    return 0;
56
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章