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. 关于GVIM的配置。

    最近开始考虑从常用的 Dev-Cpp 和 Visual Studio 开发环境中转到gvim+gcc+gdb. 我一直觉得IDE很好方便,不是很明白别人这样的配置.但还是开始有意识的去尝试更换环境. ...

  2. ContentPlaceHolderID属性

    用来对应包含与当前内容关联的 ContentPlaceHolder 的 ID啊 说白了就是去找母版页相应的ContentPlaceHolder ,然后把内容扔进那里面去 <asp:Content ...

  3. hdu_2141_Can you find it?(二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题意:叫你找三个数,使得这三个数的值为X,如果找得到就输出YES否则输出NO,注意两个32位数相 ...

  4. 弹出框,先弹出遮罩有透明度灰色100%高宽,置顶z-index:999---再弹出框最顶部z-index:9999

    <div class="mask"></div> <div class="maskbox"> <form id=&qu ...

  5. if语句,函数function

    1.语句 一般分为顺序,分支和循环语句. if是分支语句 格式1:if(){}  若满足就进入花括号,若不满足就跳过 格式2:if(){}else(){}二选一.若if满足则else一定不执行,反之则 ...

  6. server服务器信息页面添加步骤

    1. 在数据库更新链接 /portal/server/getServerList 2. 写实体类 Server.java 3. 写Server.hbm.xml <?xml version=&qu ...

  7. masonry使用问题

    2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...

  8. iperf linux版本移植到android (使用工具链方式不是使用Android.mk)

    由于很多程序是用makefile编译linux应用程序的,如果移植到android就要重新写Android.mk,对于不熟悉这个的人来说,特别麻烦,所以这里介绍只修改makefile就能移植到andr ...

  9. CentOS 安装及配置Salt api

    安装 # yum -y install salt-api 更改配置 vim /etc/salt/master 把默认的注释掉  我们这边默认配置文件目录是/srv/salt default_inclu ...

  10. HDU5907 Find Q 数学

    题目大意:求当前串中只含q的连续子串的个数 题目思路:水题,但要注意的是计算过程中可能超int范围; #include<iostream> #include<algorithm> ...