701C codeforces - They are everywhere
Solution in C++
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define pii pair<int,int>
int n,sol,all,l,r;
string inp;
map<int,int> wor;
set<char> uni;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>inp;
    for(auto c : inp)uni.insert(c);
    all=uni.size();
    sol=n;
    while(1)
    {
        if(wor.size()<all && r<n)
            wor[ inp[r] ]++,
            r++;
        else if(wor.size()==all && l<r)
        {
            sol=min(sol,r-l);
            if(--wor[inp[l]]==0)wor.erase(inp[l]);
            l++;
        }
        else break;
    }
    cout<<sol;
    return 0;
}
0 التعليقات:
إرسال تعليق