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. HDU 2177 取(2堆)石子游戏 (威佐夫博弈)

    题目思路:威佐夫博弈: 当当前局面[a,b]为奇异局时直接输出0 否则: 1.若a==b,输出(0 0): 2.将a,b不停减一,看能否得到奇异局,若有则输出: 3.由于 ak=q*k(q为黄金分割数 ...

  2. POJ 2411 Mondriaan's Dream/[二进制状压DP]

    题目链接[http://poj.org/problem?id=2411] 题意:给出一个h*w的矩形1<=h,w<=11.用1*2和2*1的小矩形去填满这个h*w的矩形,问有多少种方法? ...

  3. [Rails] 设计模式 MVC

    使用 MVC 的Web开发框架有很多,比如 PHP 的 Zend,Python 的 Django 和 Golang 的 Beego.这篇文章主要介绍一下 MVC 的基本概念. 何为MVC 为了明确一个 ...

  4. Maven+STS工程中Maven Dependencies 文件夹丢失问题

    在我们使用Maven+sts工程中偶尔会出现这种情况: Maven Dependencies文件夹在新打开的工程中丢失,造成 web project 自动编译出错,缺少必要的库文件: 如下图所示的情况 ...

  5. scala占位符_的用法

    占位符的用途颇多,先介绍几种常用的用法 1.作为"通配符",类似Java中的*.如import scala.math._2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序 ...

  6. eclipse怎么连接到MySQL中的表!!!!!

    简介: 用eclipse编写的好的代码,我们怎么才能连接到数据库呢?对于初学者,特别是在连接数据库这块经常会发生一些莫名的错误,一般来说,归根是我们连接数据库这一过程发生错误.那么我们如何来解决呢?那 ...

  7. ACdream 1028 Path

    先思考一下序列上应该怎么做. 如果某段和为x,并且x为偶数,那么比x小的偶数,一定是这段的子段. 如果某段和为x,并且x为奇数,那么比x小的奇数,一定是这段的子段. 因此....只要寻找最大的连续的和 ...

  8. JAVASCRIPT 框架>>

    jQuery jQueryjQuery 是目前最受欢迎的 JavaScript 框架 jQuery 是为处理 HTML 事件而特别设计的<script type="text/javas ...

  9. Andorid第三方库

    博客: http://blog.csdn.net/codywangziham01/article/details/11818559#t8 RoboGuice: http://www.importnew ...

  10. LeadTools答题卡识别方案

    /// <summary> /// 批改操作 /// </summary> public AnswerCard DoCorrect(Stream AnserCardFile) ...