Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- const int INF = 1e9 + 10;
- const int MAXN = 2e5 + 10;
- int cnt1[MAXN], cnt2[MAXN], cnt3[MAXN];
- void solve(){
- string s;
- cin >> s;
- int n = s.size();
- s = "#" + s;
- cnt1[0] = 0;
- cnt2[0] = 0;
- cnt3[0] = 0;
- for (int i = 1; i <= n; i++){
- if (s[i] == '1'){
- cnt1[i] = cnt1[i - 1] + 1;
- }
- else{
- cnt1[i] = cnt1[i - 1];
- }
- if (s[i] == '2'){
- cnt2[i] = cnt2[i - 1] + 1;
- }
- else{
- cnt2[i] = cnt2[i - 1];
- }
- if (s[i] == '3'){
- cnt3[i] = cnt3[i - 1] + 1;
- }
- else{
- cnt3[i] = cnt3[i - 1];
- }
- }
- int ans = n + 1;
- for (int l = 1; l <= n; l++){
- int left = l;
- int right = n;
- int pos = 0;
- while(left <= right){
- int mid = (left + right) / 2;
- if (cnt1[mid] - cnt1[l - 1] > 0 &&
- cnt2[mid] - cnt2[l - 1] > 0 &&
- cnt3[mid] - cnt3[l - 1] > 0){
- pos = mid;
- right = mid - 1;
- }
- else{
- left = mid + 1;
- }
- }
- if (pos != 0){
- ans = min(pos - l + 1, ans);
- }
- }
- if (ans == n + 1){
- cout << 0 << "\n";
- }
- else{
- cout << ans << "\n";
- }
- }
- int main()
- {
- int t = 1;
- cin >> t;
- while(t--){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment