orazma

f.cpp

Apr 28th, 2026
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4.  
  5. using namespace std;
  6. const int INF = 1e9 + 10;
  7. const int MAXN = 2e5 + 10;
  8. int cnt[5];
  9.  
  10. void solve(){
  11.     string s;
  12.     cin >> s;
  13.     int n = s.size();
  14.     s = "#" + s;
  15.  
  16.     int ans = n + 1;
  17.     int right = 0;
  18.     int k = 0;
  19.     for (int left = 1; left <= n; left++){
  20.         while (right < n && k < 3){
  21.             right++;
  22.             cnt[s[right] - '0']++;
  23.             if (cnt[s[right] - '0'] == 1){
  24.                 k++;
  25.             }
  26.         }
  27.  
  28.         if (k == 3){
  29.             ans = min(ans, right - left + 1);
  30.         }
  31.  
  32.         cnt[s[left] - '0']--;
  33.         if (cnt[s[left] - '0'] == 0){
  34.             k--;
  35.         }
  36.     }
  37.     if (ans == n + 1){
  38.         cout << 0 <<"\n";
  39.     }
  40.     else
  41.         cout << ans << '\n';
  42.  
  43. }
  44.  
  45. int main()
  46. {
  47.  
  48.     int t = 1;
  49.     cin >> t;
  50.  
  51.     while(t--){
  52.         solve();
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment