codeforces Another Problem on Strings 记录一个不慎

题意:输入一个二进制数,看其中有K个1 的字串数。
组合数学的思想要多用啊...
#include<stdio.h>
#include<iostream>
#define LMT 1000003
//要有一个变大
using namespace std;
long long q[LMT],k;
int head,tail;
int main()
{
    int i=0,sum=0;
    char a;
    head=tail=0;
    q[tail++]=-1;
    int l=0;
    long long ans=0;
    scanf("%d",&k);getchar();
    while((a=getchar())&&(a=='1'||a=='0'))
    {
        a-='0';
       if(k>0)
       {
        if(a)
        {
            q[tail++]=i;
            sum++;
            if(sum>k)
            {
              ans+=(q[head+1]-q[head])*(q[tail-1]-q[tail-2]);
               sum--;
               head++;
            }
        }
       }
       else
       {
           if(!a)
           ans+=++l;
           else l=0;
       }
        i++;
    }
    if(sum==k&&k)ans+=(i-q[tail-1])*(q[head+1]-q[head]);
    cout<<ans<<endl;
    return 0;
}

你可能感兴趣的:(codeforces Another Problem on Strings 记录一个不慎)