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 ;
}

最新文章

  1. h5页面唤起app(iOS和Android),没有安装则跳转下载页面
  2. 版本引发的血案check the manual that corresponds to your MySQL server version for the right syntax
  3. 01.AFNetworking原理及常用操作
  4. 在Ajax中将数组转换成字符串(0517-am)
  5. 针对PCB飞针测试快速有效的技巧
  6. CSS3阴影 box-shadow的使用和技巧总结[转]
  7. Swift语言指南(三)--语言基础之整数和浮点数
  8. vultr优惠码ssd vps赠送50美金,长期有效
  9. js获取json的value
  10. 百度,google的地理编码
  11. TSP问题 遗传算法 智能优化算法
  12. Cmd Markdown 学习
  13. Codeforces103D - Time to Raid Cowavans
  14. C++ Primer 有感(命名空间)
  15. 【学亮IT手记】jQuery DOM操作-获取内容和属性
  16. OC仿支付宝输入UITextField输入车牌号
  17. 获取本机的ip地址(排除虚拟机,蓝牙等ip)
  18. python中闭包和装饰器的理解(关于python中闭包和装饰器解释最好的文章)
  19. Elasticsearch源码分析 | 单节点的启动和关闭
  20. vscode编辑器配置C语言编译运行环境

热门文章

  1. SpringBoot入门篇--热部署
  2. VC字符串转换常用函数
  3. Python——连接操作数据库
  4. MNIST数据集入门
  5. OpenACC Julia 图形
  6. 使用Teleport Ultra批量克隆网站,使用Easy CHM合并生成chm文件
  7. Java 8 日期时间API
  8. 16 python 异常处理
  9. UI5-文档-4.17-Fragment Callbacks
  10. Spring MVC国际化