fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void sol() {
  5. int n, k; cin >> n >> k;
  6. string s; cin >> s;
  7. s = "X" + s + "X"; // ---> Nãy anh quên nói, chỗ này em phải thêm
  8. // 2 cái "X" ở đầu với cuối để làm biên tránh check thiếu
  9. vector <int> a;
  10.  
  11. for (int i = 0; i < s.size(); i++) {
  12. if (s[i] == 'X') a.push_back(i);
  13. }
  14.  
  15. // int ans1 = (a[1 + k] - 1) - ((a[1 - 1] + 1) + 1); --> Không cần, check trong for luôn
  16. int res = 0;
  17. // i phải = 1 để check từ đầu (Còn 0 là cái căn nhà mình làm biên ở dòng 7)
  18. // i < a.size() - k - 1 (trừ cho k - 1 để tránh check tràn mảng)
  19. for (int i = 1; i <= a.size() - k - 1; i++) {
  20. res = max(res, (a[i + k] - 1) - (a[i - 1] + 1) + 1);
  21. }
  22. cout << res << endl;
  23. }
  24.  
  25. int main()
  26. {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(NULL);
  29. cout.tie(NULL);
  30.  
  31. //freopen("LAND.inp", "r", stdin);
  32. //freopen("LAND.out", "w", stdout);
  33.  
  34. sol();
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
0