题目链接
题目描述的是抓取精灵,转化一下,就是问在一个长度是n的字符串里面,你选择最短的字串,使得这个字串包涵该字符串中所有的字母至少一次。
假设我们知道区间[s,t]的不同字母个数,那么可以计算出[s+1,t1],那么t1>=t, 也就是直接尺取法搞一下就好了
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<bits/stdc++.h>
#include<map>
#include<string>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd
#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)
const int maxn = 1e5+200;
const LL inf = 1LL << 34;
char str[maxn];
set<int> s;
map<int,int> mp;
int main(){
int n;scanf("%d%s",&n,str);
for(int i=0;i<n;i++)s.insert(str[i]-'a');
int S = s.size();
int st=0,en=0,num=0;
int ans=n;
while(true){
while(en<n&&num<S){
if(mp[str[en++]-'a']++==0){
num++;
}
}
if(num<S)break;
ans = min(ans,en-st);
if(--mp[str[st++]-'a']==0)num--;
}
printf("%d\n",ans);
return 0;
}