fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(0);
  9. int d, k ,n, x, maks, xd; char c;
  10. cin >> d;
  11. while (d--)
  12. {
  13. cin >> n >> k;
  14. vector <int> y;
  15. y.push_back(1);
  16. x=maks=0;
  17.  
  18. for (int i=0; i<n; i++)
  19. {
  20. cin >> c;
  21. if(c=='1') {x++; y.push_back(1);}
  22. else y[x]++;
  23. }
  24.  
  25. if (y.size()==1) cout << n << endl;
  26. else
  27. {
  28. xd=0;
  29. for (int j=0; j<=k; j++) xd+=y[j];
  30. maks=xd;
  31. for (int i=k; i<y.size()-1; i++)
  32. {
  33. xd+=y[i+1]; xd-=y[i-k];
  34. if(xd>maks) maks=xd;
  35. }
  36. cout << maks-1 << endl;
  37. }
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 4508KB
stdin
8
10 1
0101000001
12 2
010110101001
15 3
101000011100100
10 0
0000000000
10 1
0000000000
10 3
0010101000
10 4
0010101000
10 3
0101010101
stdout
7
6
9
10
10
10
10
7