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. ajax控制页面跳转

    一开始我是这么写的,一直报错,跳转路径解析不了,显示为问号: 前台html: <form> <table style="margin: 200px auto;"& ...

  2. TCP/IP协议和IP

    理解 使用网络能够把多方链接在一起,然后可以进行数据传递 所谓的网络编程就是,让在不同的电脑上的软件能够进行数据传递,即进程之间的通信 tcp/ip简介 1. 什么是协议 有的说英语,有的说中文,有的 ...

  3. HTML5 Canvas ( 图形的像素操作 ) getImageData, putImageData, ImgData.data

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. ajax的一些小知识

    eval()可以把一个字符串转化为本地的js代码来执行 <script type="text/javascript"> var str = "alert('h ...

  5. java Export Excel POI 转

    最终选择用POI成功导出excel.总之很有用. http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html http://poi ...

  6. 使用SendMessage进行进程间通信

    Imports System.Runtime.InteropServices Public Class Monitor <DllImport("user32.dll", Ch ...

  7. Invalid args, too big block

    在调试vs程序的时候 查看log 会出现下面几行log: [9064] Error - [9064] Invalid args, too big block[9064] [9064] Error - ...

  8. UI5-文档-4.25-Sorting and Grouping

    为了使我们的发票列表更加用户友好,我们将它按字母顺序排序,而不是仅仅显示来自数据模型的顺序.此外,我们还引入了组,并添加了发布产品的公司,以便更容易使用数据. Preview The list is ...

  9. eclipse使用ctrl+shift+F格式化代码失效

    通常情况出现这种问题是组合快捷键和别的软件快捷键冲突了, 最常见的是和搜狗输入法冲突, 在设置中找到搜狗输入法然后把冲突的快捷键取消掉就可以了.

  10. Flex 布局教程:实例

    分类: 开发者手册 Flex 布局教程:实例篇   作者: 阮一峰 日期: 2015年7月14日 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Fle ...