题目链接:http://codeforces.com/problemset/problem/165/C点击打开链接
用num【i】记录从字符串首开始有i个1字符串的个数
然后用前缀思想计算ans值
#include <bits/stdc++.h>
using namespace std;
int num[1111111];
int main()
{
long long int ans=0;
int k;
cin >> k;
string s;
cin >> s;
num[0]=1;
int mid=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='1')
mid++;
if(mid>=k)
ans+=num[mid-k];
num[mid]++;
}
cout << ans <<endl;
}