CF #364 (Div. 2)(C. They Are Everywhere 尺取法)

题目链接
题目描述的是抓取精灵,转化一下,就是问在一个长度是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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值