首页 > 其他 > 详细

!Codeforces Round #364 (Div. 2) C. They Are Everywhere

时间:2020-01-16 21:33:04      阅读:76      评论:0      收藏:0      [点我收藏+]

https://codeforces.com/contest/701/problem/C

 

 

binary search strings two pointers

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
char s[N];

int main(){
    int n,tot=0;
    cin>>n;
    getchar();
    map<char,int>cnt;
    for(int i=1;i<=n;i++){
    s[i]=getchar();
    if(!cnt[s[i]])tot++;
    cnt[s[i]]++;}
    cnt.clear();
    int r=0,ntot=0,ans=1e5+5,flag=0;
    for(int i=1;i<=n;i++){
    //    if(i!=1&&cnt[s[i-1]]==1) ntot--,cnt[s[i-1]]--;
    if(i!=1){
        if(cnt[s[i-1]]==1)ntot--;
        cnt[s[i-1]]--;
    }
        while(ntot<tot){
            if(r==n)break;
            r++;
            if(!cnt[s[r]])ntot++;
            cnt[s[r]]++;
        //    if(r==n){flag=1;break;    }
        }
        if(ntot==tot)ans=min(ans,r-i+1);
        //if(flag)break;
    }
    cout<<ans;
    return 0;
}

!Codeforces Round #364 (Div. 2) C. They Are Everywhere

原文:https://www.cnblogs.com/wyh447154317/p/12203279.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!