Codeforces Round #364 (Div. 2) C

来源:互联网 发布:国外windows优化软件 编辑:程序博客网 时间:2024/04/28 15:32

C They Are Everywhere

time limit per test 2 seconds
memory limit per test 256 megabytes

题面

Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won’t let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

大意

对于一个给定的字符串,找到一个最短的连续字串使得这个连续字串中包涵所有的字符。

Input

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.

The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Output

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.

Examples

input

3
AaA

output

2

input

7
bcAAcbc

output

3

input

6
aaBCCe

output

5

Note

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.

思路

记得什么人告诉我

当题目要求你求满足什么条件的最短的时候八成都是二分。

这道题显然满足二分的性质,如果我能找到一个长度为x的连续字串满足条件,那么我显然可以找到一个长度大于x的字串使之满足条件。

我们发现如果这个长度为n的串中有k个不同的字符,那么可以说答案必定位于[k,n]这个区间内,由于n是100000,那么我们用了logn的时间来二分,那么我们就必须要找到一种办法可以在O(n)的时间内完成对一个答案是否正确的判断。

显然可以找到这样的方法,假设我当前要检验的答案是m,考虑移动一个长度为m的区间从[1,m]到[n-m+1,m],通过每次从队尾剔除一个元素,再从队头加入一个元素,我们可以在O(n)的时间内检查所有一定长度的连续字串。那么这样我们就做完了。

代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<queue>#include<vector>#include<map>#include<list>#include<string>#include<ctime>#include<memory.h>#define ll long long#define _max(a,b) (((a)>(b))?(a):(b))#define _min(a,b) (((a)>(b))?(b):(a))#define MAX 100001#define MIN#define MOD 1000009#define INF 0xfffffff#define ESP 1e-8#define Pi 3.1415926535897932384626using namespace std;int n,num,b[100]={},l,r;bool p[100]={};char a[MAX];void read(){    scanf("%d\n",&n);    for(int i=1;i<=n;i++){        scanf("%c",&a[i]);        if(p[a[i]-'A']==0)            p[a[i]-'A']=1,            num++;    }    return;}void init(){    l=1,r=n;    return;}void work(){    while(l<r){        int mid=(l+r)/2,now_num=0;        memset(b,0,sizeof(b));        for(int i=1;i<=mid;i++)            if(b[a[i]-'A']++==0)                now_num++;        if(now_num==num){            r=mid;            continue;        }        bool flag=0;        for(int j=1;j<=n-mid;j++){            if(--b[a[j]-'A']==0) now_num--;            if(b[a[mid+j]-'A']++==0) now_num++;            if(now_num==num){                r=mid;                flag=1;                break;            }        }        if(flag) continue;        l=mid+1;    }    return;}void print(){    printf("%d",l);    return;}int main(){    read();    init();    work();    print();    return 0;}
0 0
原创粉丝点击