fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(NULL);
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. string s;
  11. cin >> s;
  12. int min_len = INT_MAX;
  13. int left = 0;
  14. int counts[4] = {0, 0, 0, 0};
  15. for (int right = 0; right < s.length(); ++right) {
  16. counts[s[right] - '0']++;
  17. while (counts[1] > 0 && counts[2] > 0 && counts[3] > 0) {
  18. min_len = min(min_len, right - left + 1);
  19. counts[s[left] - '0']--;
  20. left++;
  21. }
  22. }
  23. if (min_len == INT_MAX) {
  24. cout << 0 << endl;
  25. } else {
  26. cout << min_len << endl;
  27. }
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5284KB
stdin
7
123
12222133333332
112233
332211
12121212
333333
31121
stdout
3
3
4
4
0
0
4