Advertisement
miglss

3

Mar 21st, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5. int n;
  6. string s;
  7. cin >> n >> s;
  8. int posa = -22, posb = -22, posc = -22, posd = -22;
  9. int ans = -1;
  10. for (int r = 0; r < n; r++){
  11. if (s[r] == 'a') posa = r;
  12. if (s[r] == 'b') posb = r;
  13. if (s[r] == 'c') posc = r;
  14. if (s[r] == 'd') posd = r;
  15. int l = min(min(posa, posb), min(posc, posd));
  16. int le = r - l;
  17. int len = le + 1;
  18. if (l != -22){
  19. if (ans == -1 || len < ans){
  20. ans = len;
  21. }
  22. }
  23. }
  24. cout << ans << endl;
  25. }
  26.  
  27. int main(){
  28. ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); // Оптимизация
  29. solve();
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement