They Are Everywhere

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 numbern - 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.

分析:尺取法;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,mi,co[],cnt[],all;
char a[maxn];
int main()
{
int i,j,k,t;
mi=inf;
scanf("%d%s",&n,a);
rep(i,,n-)if(++co[a[i]]==)all++;
int l=-,r=,now=;
for(i=;;i++)
{
if(++cnt[a[i]]==)now++;
if(now==all)break;
}
r=i;
mi=r+;
while(l<=r&&r<n)
{
do{l++;if(--cnt[a[l]]>)mi=min(mi,r-l);else break;}while();
do{r++;}while(r<n&&++cnt[a[r]]!=);
if(r<n&&r>=l)mi=min(mi,r-l);
}
printf("%d\n",mi);
//system ("pause");
return ;
}

随机推荐

  1. ios 基础数据类型

    1:NSString http://blog.csdn.net/likendsl/article/details/7417878 http://xys289187120.blog.51cto.com/ ...

  2. unity3d继续尝试

    这一次完成了一些复杂的脚本,会了一些简单的鼠标事件,这样就能使用鼠标进行简单的交互了. 其实右边栏目上面一些奇怪的属性看的我是眼花缭乱. 也不知道干啥用的,还有就是真的很佩服里面的物理引擎确实简单易上 ...

  3. 第三十七节,hashlib加密模块

    在使用hashlib模块时需要先 import hashlib 引入模块 用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA ...

  4. Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLIC

    在mysql5中遇到的问题: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) f ...

  5. Java中的String[] args

    在每个java程序中都有一个方法,public static void main(String[] args)方法,这个参数看了好久没看懂,但是细细看来,还是挺简单的,所有的方法的参数都是一个道理,而 ...

  6. Entity Framework中对存储过程的返回值的处理

    很早就开始注意到EF了,但一直没有机会用,换了工作后,第一个项目就使用EF6进行开发. 项目不是很大,EF完全可以胜任. 但是开发过程中,难免还是会遇到一些复杂的运算,需要频繁访问数据库. 此时,想到 ...

  7. python的历史与优劣

    历史 Python的创始人是Guido van Rossum,在发明Python语言之前Guido曾参与过一门称作ABC的语言的设计,ABC是专门为非专业程序员设计的:Guido在Python语言的设 ...

  8. svg笔记----------path篇

    每个路径都必须以moveto 命令开始 moveto.lineto和closepath <path d="M 10 10 L 100 10z"/> 大写字母命令的坐标是 ...

  9. 安卓无法生成R文件原因

    原因个人总结出来: 清单文件报错,则无法生成R文件 gen和bin目录可以删除

  10. OSPF 原理

    关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...