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. 【第二篇】学习 android 事件总线androidEventbus之异步事件的传递

    1,不同Activity直接发送Ansy的事件,以及其他任何事件,必须通过 postSticky方式来进行事件的传递,而不能通过post的形式来进行传递:EventBus.getDefault().p ...

  2. <转>如何高效快速看懂Android源码

    原网址:http://jingyan.baidu.com/article/574c5219ca78ed6c8d9dc12a.html 在Android系统上工作了一段时间,经常会遇到题目中的问题,下面 ...

  3. 第3章 Java语言基础----声明常量

    在程序运行过程中一直不会改变的量称为常量(constant),通常也被称为“final变量”: 声明常量的语句:final 数据类型 常量名[=值] 备注:1)常量名通常使用大写字母,但并不是必须的; ...

  4. AS3.0中用于网络通信的类总结

      0条评论  1.Loader Loader 类可用于加载 SWF 文件或图像(JPG.PNG 或 GIF)文件.也只能是这些对象. 网络协议基础:HTTP/RTMP 加载方法原型:Loader.l ...

  5. Java 学习路线

    java 入门到精通  转自:http://forum.hibernate.org.cn作者:robbinJava Learning Path (一).工具篇 一. JDK (Java Develop ...

  6. Lvs+Keepalived+Bind+web构建高可用负载均衡系统

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://hatech.blog.51cto.com/8360868/1417899 --- ...

  7. Spring 实例化bean的三种方式:

    方法一:使用构造器实例化bean java代码: package com.model; public class User { private String username; public User ...

  8. centos主机信任

    一.实现原理 使用一种被称为"公私钥"认证的方式来进行ssh登录."公私钥"认证方式简单的解释是: 首先在客户端上创建一对公私钥(公钥文件:~/.ssh/id_ ...

  9. sql语句按月份统计查询

    select year(createdate) 年,month(createdate) 月,count(1) from public_cms_arcwhere (userid in (select i ...

  10. make 命令执行时,报错“missing separator stop”

    在Makefile文件中,命令必须以[tab]键开始.