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.

题目链接:http://codeforces.com/contest/701/problem/C


题意:输出包含所有出现过的字母的最短子串的长度。

思路:设子串的开始为s=1,结束为当前字母的位置。统计每种字母出现的次数。如果s位置的字母出现的次数大于1将s后移直至不能移动为止。设ans为输出的答案,判断当前i-s+1与ans的大小。如果小的话ans=i-s+1。

代码:

#include<bits/stdc++.h>
using namespace std;
char let[];
int cou[];
int main()
{
int i,n;
scanf("%d",&n);
getchar();
memset(cou,,sizeof(cou));
int ans=;
int s=;
for(i=; i<=n; i++)
{
scanf("%c",&let[i]);
if(cou[let[i]-'A']==) ans=i-s+;
cou[let[i]-'A']++;
while(s<i)
{
if(cou[let[s]-'A']>)
{
cou[let[s]-'A']--;
s++;
}
else break;
}
if(ans>i-s+) ans=i-s+;
}
cout<<ans<<endl;
return ;
}

Codeforces 701C. They Are Everywhere 思路题的更多相关文章

  1. CodeForces 606C--Sorting Railway Cars,思路题~~~

    C - Sorting Railway Cars   Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  2. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  3. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  4. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

  5. http://codeforces.com/gym/100623/attachments E题

    http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...

  6. POJ 1904 思路题

    思路: 思路题 题目诡异地给了一组可行匹配 肯定有用啊-. 就把那组可行的解 女向男连一条有向边 如果男喜欢女 男向女连一条有向边 跑一边Tarjan就行了 (这个时候 环里的都能选 "增广 ...

  7. BZOJ 3252: 攻略(思路题)

    传送门 解题思路 比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次..其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的 ...

  8. BZOJ 1303: [CQOI2009]中位数图(思路题)

    传送门 解题思路 比较好想的思路题.首先肯定要把原序列转化一下,大于\(k\)的变成\(1\),小于\(k\)的变成\(-1\),然后求一个前缀和,还要用\(cnt[]\)记录一下前缀和每个数出现了几 ...

  9. http://codeforces.com/gym/100623/attachments H题

    http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...

随机推荐

  1. Java操作Sqoop对象

    Windows下使用Eclipse工具操作Sqoop1.4.6对象 Sqoop是用来在关系型数据库与Hadoop之间进行数据的导入导出,Windows下使用Eclipse工具操作时,需要先搭建好Had ...

  2. ES6进一步整理

    内容: 1.变量及赋值 2.函数 3.数组及json 4.字符串 5.面向对象 6.Promise 7.generator 8.模块 1.变量及赋值 (1)ES5变量定义 var:     可以重复定 ...

  3. uva-10004-俩色图验证

    题意: 在1976年,四色猜想被一个计算机助手提出,这个理论表示对任意一个地图的上色都只需要四种颜色,地图内每一个区块和相邻的区块颜色都不相同.你现在被要求解决一个相似但相对简单的问题.给你任意一个连 ...

  4. 《汇编语言 基于x86处理器》前五章的小程序

    ▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...

  5. H5-DataUrl FileReader

    DataUrl: 将二进制文件流以字符串的形式存在,如果是图片可以在页面上展示.经常用于Canvas截图或画图展示用. 格式:data:image/png;base64,****. /** * dat ...

  6. DrawDib 使用例子<转>

    #include<vfw.h>#pragma comment(lib,"Vfw32.lib") BITMAPINFOHEADER biHeader; memset(&a ...

  7. Git----分支管理之解决冲突03

    人生不如意之事十之八九,合并分支往往也不是一帆风顺. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1Switched to a new br ...

  8. 搭建turbine时 hystrix MaxConcurrentConnections reached 异常

    2017-03-28 10:04:47.438 ERROR 1035 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor : Co ...

  9. 编写ios和android共用的c/c++库时 使用iconv的问题(转)

    因为在项目中需要同时维护ios和Android,不同的代码不利于开发的便捷和以后的维护,所以在最近的一个项目中,两种手机应用的通信部分打算使用c/c++库来统一编写,ios调用.a静态库,androi ...

  10. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...