zzulioj 2570:找子串(尺取)

练习一下刚刚学习的尺取法

#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#define TP 233333333333333
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 10005;
char str[maxn];
int cnt[30];
bool check() {  //判断是否选取区段是否包含全部26个字母
	for (int i = 0; i<26; i++)
		if (!cnt[i] || cnt[i] < 0)
			return true;
	return false;
}
int main(void) {
	while(gets(str)) {
		memset(cnt, 0, 30*sizeof(int));
		int kase = 0, start = 0, _min = INT_MAX;
		ll t = TP;
		while(t--) {
			while(check()) { //选取一段满足条件的字符串,起始位置为start, 终止位置为kase
				cnt[str[kase++] - 'A']++;
				if (kase >= strlen(str))
					break;
			}
			if (check()) //如果该段没有满足条件的字符串直接跳出
				break;
			_min = min(_min, kase-start); 
			cnt[str[start++] - 'A']--; //让左边的起点往右移动一位
			}
			if (_min != INT_MAX)
				cout << _min << endl;
			else 
				cout << "NO\n";
		}
    return 0;
}
原文地址:https://www.cnblogs.com/shuitiangong/p/12266527.html