题目描述:
主要思路:
开始想着用双指针做吧,写着写着发现不会写了,后来看了dalao的题解之后,发现,这是一道前缀和且需要特判k=0的题目。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll sum[N];
unordered_map<ll,ll> book;
int main()
{
int k;
cin>>k;
string s;
cin>>s;
ll now=0;
ll ans=0;
book[0]=1;
if(k!=0)
{
for(int i=0;i<s.size();i++)
{
if(s[i]=='1') now+=1;
book[now]+=1;
ans+=book[now-k];
// cout<<now<<' '<<i<<' '<<ans<<endl;
}
}
else
{
for(int i=0;i<s.size();i++)
{
if(s[i]=='1')
{
ans+=(now+1)*now/2;
now=0;
}
else now+=1;
}
ans+=(now+1)*now/2;
}
cout<<ans<<endl;
return 0;
}