AcWing 3624. 三值字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200001;
int T;
char s[N];
int cnt[3];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%s", &s);
int n = strlen(s);
int ans = n + 1;
memset(cnt, 0, sizeof cnt);
for (int i = 0, j = 0; j < n; ++j) {
++cnt[s[j] - '1'];
while (cnt[s[i] - '1'] > 1)
--cnt[s[i++] - '1'];
if (cnt[0] && cnt[1] && cnt[2])
ans = min(ans, j - i + 1);
}
printf("%d\n", ans == n + 1 ? 0 : ans);
}
return 0;
}