fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int countSubarraylessThanK(string s,int k){
  6. int n = s.size();
  7. int i = 0 , j = 0 ;
  8. int count = 0 ;
  9. int ans = 0 ;
  10. while(j<n){
  11. if(s[j]=='5')count++;
  12. while(count>k){
  13. if(s[i] == '5')
  14. count--;
  15. i++;
  16. }
  17. ans+=j-i+1;
  18. j++;
  19. }
  20. return ans;
  21. }
  22. int main() {
  23. string s ;
  24. cin>>s;
  25. int k ; cin>>k;
  26. int n = s.size();
  27. int ans = countSubarraylessThanK(s,k) - countSubarraylessThanK(s,k-1);
  28. cout<<ans;
  29. // your code goes here
  30. return 0;
  31. }
Success #stdin #stdout 0s 5320KB
stdin
5055235
2
stdout
8