fork download
  1. //https://o...content-available-to-author-only...u.vn/problem/substr1
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. const long long MaxN = 1e6 + 5;
  5. long long k,prefix[MaxN];
  6. string s;
  7. int main()
  8. {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0);
  11. cin >> k;
  12. cin >> s;
  13. s=" " + s;
  14. for (long long i=1; i<s.size(); i++)
  15. {
  16. prefix[i]=prefix[i-1];
  17. if(s[i]=='1')
  18. {
  19. prefix[i]++;
  20. }
  21. }
  22. long long res=0;
  23. for (long long i=1; i<s.size(); i++)
  24. {
  25. if (prefix[i] - k >= 0)
  26. {
  27.  
  28. long long t1 = lower_bound(prefix, prefix + i, prefix[i] - k) - prefix;
  29. long long t2 = upper_bound(prefix, prefix + i, prefix[i] - k) - prefix;
  30.  
  31.  
  32. res += (t2 - t1);
  33. }
  34. }
  35. cout << res;
  36. }
  37.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty