fork(1) 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. count--;
  14. i++;
  15. }
  16. ans+=j-i+1;
  17. j++;
  18. }
  19. return ans;
  20. }
  21. int main() {
  22. string s ;
  23. cin>>s;
  24. int k ; cin>>k;
  25. int n = s.size();
  26. int ans = countSubarraylessThanK(s,k) - countSubarraylessThanK(s,k-1);
  27. cout<<ans;
  28. // your code goes here
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5320KB
stdin
5055235
2
stdout
5