Educational Codeforces Round 87 (Rated for Div. 2) B. Ternary String滑动窗口板子题

B. Ternary String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once.

A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s.

Input
The first line contains one integer t (1≤t≤20000) — the number of test cases.

Each test case consists of one line containing the string s (1≤|s|≤200000). It is guaranteed that each character of s is either 1, 2, or 3.

The sum of lengths of all strings in all test cases does not exceed 200000.

Output
For each test case, print one integer — the length of the shortest contiguous substring of s containing all three types of characters at least once. If there is no such substring, print 0 instead.

Example
inputCopy
7
123
12222133333332
112233
332211
12121212
333333
31121
outputCopy
3
3
4
4
0
0
4
Note
Consider the example test:

In the first test case, the substring 123 can be used.

In the second test case, the substring 213 can be used.

In the third test case, the substring 1223 can be used.

In the fourth test case, the substring 3221 can be used.

In the fifth test case, there is no character 3 in s.

In the sixth test case, there is no character 1 in s.

In the seventh test case, the substring 3112 can be used.



多个询问,每个询问给一个 2 e 5 2e5 2e5的字符串 S S S, S S S只包含数字 ′ 1 ′ , ′ 2 ′ , ′ 3 ′ '1','2','3' 1,2,3
求一个连续的子串 s u b S subS subS,要求 s u b S subS subS最短,并且包含 ′ 1 ′ , ′ 2 ′ , ′ 3 ′ '1','2','3' 1,2,3的每个数字

滑动窗口板子题 :
类似洛谷"唯一的雪花"那题

//#define debug
#ifdef debug
#include 
#include "/home/majiao/mb.h"
#endif


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN ((int)2e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \
do { \
	cout << "\033[31;1m " << #x << " -> "; \
	err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif

template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

int n, m, Q, K;
char buf[MAXN];

#define INC (cnt[(int)'1'] && cnt[(int)'2'] && cnt[(int)'3'])

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%d ", &Q);
	while(Q--) {
		scanf("%s ", buf);
		int cnt[128] = { 0 };
		int lef = 0, rig = -1, ans = 0x3f3f3f3f;
		while(buf[lef]) {
			while(buf[rig+1] && !INC) {
				cnt[(int)buf[rig+1]] ++;
				rig ++;
			}
//			show(lef, rig, cnt[(int)'1'], cnt[(int)'2'], cnt[(int)'3']);
			if(INC)
				ans = min(ans, rig-lef+1);
			cnt[(int)buf[lef]] --;
			lef ++;
		}
		printf("%d\n", ans==0x3f3f3f3f ? 0 : ans);
	}

#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


你可能感兴趣的:(滑动窗口,尺取法)