Another Problem on Strings-(CodeForces-165C)

      *C. Another Problem on Strings*

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A string is binary, if it consists only of characters “0” and “1”.

String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string “010” has six substrings: “0”, “1”, “0”, “01”, “10”, “010”. Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.

You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters “1”.

Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.

Output
Print the single number — the number of substrings of the given string, containing exactly k characters “1”.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
inputCopy
1
1010
outputCopy
6
inputCopy
2
01010
outputCopy
4
inputCopy
100
01010
outputCopy
0
Note
In the first sample the sought substrings are: “1”, “1”, “10”, “01”, “10”, “010”.

In the second sample the sought substrings are: “101”, “0101”, “1010”, “01010”.
思路:第一次写博客,这题想了好久,得有一天了,也看了很多别人的博客,自己写的时候一直被T,应该是方法错了,一步一步来嘛,不会思路,慢慢学吧。
不感慨了,说题,有一个字符串,只有0/1,给定k值,这个字符串中有多少满足(字串中1的个数为k)的子串,输出出来。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e6+9;
char s[N];//字符串
int ch[N];//第i个1后面跟了几个0,再+1,ch[1]=2,
//第1个1后面跟了1个0,+1=2;
int main ()
{
    int k;
    scanf("%d",&k);
    getchar();
    gets(s);
    int n=strlen(s);
    int num=0;
    for(int i=0;i<n;i++){
        if(s[i]=='1') num++;
    }
    if(k>num) printf("0\n");//如果有0的情况特判一下
    else{
        ll ans=0;
        int num1=0;
        ch[0]=1;//因为字符串可以包含本身这一个单的串,所以要在ch[0]=1,特殊的初始化
        for(int i=0;i<n;i++){//遍历每个位置
            if(s[i]=='1'){
                num1++;//统计1的个数
            }
            if(num1>=k) ans=ans+ch[num1-k];
            //刚出现的1的个数>=k,ch[num1-k]当前的1的总个数-k的个数,
            //k个1的字符串,往前倒到k+1个1之间的0的数目就是满足条件 的子串数目,
            //101001,k=2,第三个1的位置最后一位,满足条件的1001固定,到它前面去找1,
            //ch[num1-k]=ch[3-2]=num[1],1后面有几个0,就是有几种情况,所以num1-k,就是看当前num1与k之间差多少,就让当前位置往前倒多少,
            //k=1,ch[num1-k]=ch[3-1]=ch[2],就是看2到3之间有几个0,那么固定的字符串,就可以往前有多少0,就有多少情况
            ch[num1]++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

101001
k=1;
num[0]=1
num[1]=1 1 ans=0+num[1-1]=0+num[0]=1
num[1]=2; 10 ans=1+num[1-1]=1+num[0]=2
num[2]=1 101 ans=2+num[2-1]=2+2=4
num[2]=2 1010 ans=4+num[2-1]=4+2=6
num[2]=3 10100 ans=6+num[2-1]=6+2=8
num[3]=1 101001 ans=8+num[3-1]=8+3=11

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值