题目
题意:
给你一个字符串 s s s,问里面字串出现 1 , 2 , 3 1,2,3 1,2,3的最短序列是多少。
思路:
我们对于每次 1 , 2 , 3 1,2,3 1,2,3的时候,都去最晚出现的时候,然后我们在这些中每次三个中最大距离最小,这个就是答案了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void out(ll x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 2e5 + 10;
const int inf = 1e9;
char s[maxn];
int main() {
int t;
read(t);
while (t--) {
int cnt1 = inf, cnt2 = inf, cnt3 = inf;
scanf("%s", s);
int len = strlen(s), Min = inf;
for (int i = 0; i < len; i++) {
if (s[i] == '1') cnt1 = i;
if (s[i] == '2') cnt2 = i;
if (s[i] == '3') cnt3 = i;
Min = min(max(cnt1, max(cnt2, cnt3)) - min(cnt1, min(cnt2, cnt3)) + 1, Min);
}
if (cnt1 != inf && cnt2 != inf && cnt3 != inf) out(Min);
else out(0);
putchar('\n');
}
return 0;
}