orazma

f.cpp

Apr 28th, 2026
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 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 cnt1[MAXN], cnt2[MAXN], cnt3[MAXN];
  9.  
  10. void solve(){
  11.     string s;
  12.     cin >> s;
  13.     int n = s.size();
  14.     s = "#" + s;
  15.  
  16.     cnt1[0] = 0;
  17.     cnt2[0] = 0;
  18.     cnt3[0] = 0;
  19.     for (int i = 1; i <= n; i++){
  20.         if (s[i] == '1'){
  21.             cnt1[i] = cnt1[i - 1] + 1;
  22.         }
  23.         else{
  24.             cnt1[i] = cnt1[i - 1];
  25.         }
  26.         if (s[i] == '2'){
  27.             cnt2[i] = cnt2[i - 1] + 1;
  28.         }
  29.         else{
  30.             cnt2[i] = cnt2[i - 1];
  31.         }
  32.         if (s[i] == '3'){
  33.             cnt3[i] = cnt3[i - 1] + 1;
  34.         }
  35.         else{
  36.             cnt3[i] = cnt3[i - 1];
  37.         }
  38.     }
  39.     int ans = n + 1;
  40.     for (int l = 1; l <= n; l++){
  41.         int left = l;
  42.         int right = n;
  43.         int pos = 0;
  44.         while(left <= right){
  45.             int mid = (left + right) / 2;
  46.             if (cnt1[mid] - cnt1[l - 1] > 0 &&
  47.                 cnt2[mid] - cnt2[l - 1] > 0 &&
  48.                 cnt3[mid] - cnt3[l - 1] > 0){
  49.                     pos = mid;
  50.                     right = mid - 1;
  51.             }
  52.             else{
  53.                 left = mid + 1;
  54.             }
  55.         }
  56.         if (pos != 0){
  57.             ans = min(pos - l + 1, ans);
  58.         }
  59.     }
  60.     if (ans == n + 1){
  61.         cout << 0 << "\n";
  62.     }
  63.     else{
  64.         cout << ans << "\n";
  65.     }
  66. }
  67.  
  68. int main()
  69. {
  70.  
  71.     int t = 1;
  72.     cin >> t;
  73.  
  74.     while(t--){
  75.         solve();
  76.     }
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment