fork download
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n, k;
  8. cin >> n >> k;
  9.  
  10. string s;
  11. cin >> s;
  12.  
  13. unordered_map<int, int> g;
  14. g[0] = 1;
  15.  
  16. int count = 0;
  17. int sum = 0;
  18.  
  19. for (int j = 0; j < n; j++)
  20. {
  21. if (s[j] == '5')
  22. {
  23. count = count + 1;
  24. }
  25.  
  26. int v = count - k;
  27. sum = sum + g[v];
  28.  
  29. g[count] = g[count] + 1;
  30. }
  31.  
  32. cout << sum <<endl;
  33. }
  34.  
  35. /*
  36.   input
  37.   7
  38.   2
  39.   0050501
  40.  
  41. Index: 0 1 2 3 4 5 6
  42. String: 0 0 5 0 5 0 1
  43. The two '5' characters occur at indices 2 and 4.
  44. A substring must start at or before index 2 and end at or after index 4, while excluding any additional '5' characters. There are:
  45. - 3 choices for the starting index: 0, 1, 2
  46. - 3 choices for the ending index: 4, 5, 6
  47.  the answer is 3 * 3 = 9
  48.  
  49.   */
  50.  
Success #stdin #stdout 0s 5312KB
stdin
7
2
0050501
stdout
9