C. They Are Everywhere
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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<cstdio>
#include<cstring>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
char a[];
int vis[]; inline int getid(char x){
if(x>='A'&&x<='Z')return x-'A';
return x-'a'+;
}
int main(){
int n,all=;
scanf("%d",&n);
scanf("%s",a+);
F(i,,n)vis[getid(a[i])]=;
F(i,,)if(vis[i])all++;
memset(vis,,sizeof(vis));
int l=,r=,cnt=,ans=;
while(r<=n)
{
while(r<n&&cnt<all){
r++;
int idx=getid(a[r]);
if(vis[idx]==)cnt++;
vis[idx]++;
}
if(r==n&&cnt<all)break;
if(cnt==all){ans=min(ans,r-l+);}
while(cnt==all){
int idx=getid(a[l]);
vis[idx]--;
if(vis[idx]==)cnt--;
l++;
if(cnt==all)ans=min(ans,r-l+);
}
}
printf("%d\n",ans);
return ;
}

Codeforces Round #364 (Div. 2) C.They Are Everywhere的更多相关文章

  1. Codeforces Round #364 (Div. 2)

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  2. Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  3. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  4. 树形dp Codeforces Round #364 (Div. 1)B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  5. Codeforces Round #364 (Div. 2) B. Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round #364 (Div. 2) Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  7. Codeforces Round #364 (Div. 2) Cards

    Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...

  8. Codeforces Round #364 (Div. 2)->A. Cards

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. Codeforces Round #364 (Div. 2) E. Connecting Universities

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. Blob写入文件

    1.Spring自带方法,定义输出流就可以写入文件 final OutputStream os; os = new FileOutputStream(new File("300.zip&qu ...

  2. rsync 断点续传

    # rsync -avzP file root@172.20.7.219:/root/tmp           (我这里file指要传送的文件)

  3. ES6 之 let和const命令 Symbol Promise对象

    ECMAScript 6入门 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了. (2016年6月,发布了小幅修订的<ECMASc ...

  4. Documention

    Object.bool Does the object exist? Object.name Components share the same name with the game object a ...

  5. 《JavaScript高级程序设计》读书笔记 ---理解对象

    上一章曾经介绍过,创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示.var person = new Object();person.name = &qu ...

  6. (一)html之基本结构

    一:HTML基本结构 1.1 HTML文档结构 1.1.1 外层结构 <!DOCTYPE HTML> <html> </html> DOCTYPE元素用于告诉浏览器 ...

  7. Node.js:url

    在node运行环境中输入url: > url url { parse: [Function: urlParse], resolve: [Function: urlResolve], resolv ...

  8. mac brew install 搭建nginx php mysql

    curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1 参考 : ...

  9. 基于多线程多用户的FTP服务器与客户端功能实现

    项目介绍: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 允许用户查看当 ...

  10. 21.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E。要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak() 方法,在speak方法中输出“咿咿呀呀......”的信息。 (2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法 中输出“小样

    //Monkey类 package d922; public class Monkey { Monkey() { } Monkey (String s) { System.out.println(s) ...