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. web service初探

    概述:Web service是一个平台独立.低耦合的.自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发 ...

  2. Mac OS 修改hosts文件

    这里用得是 VI 编辑器修改 打开终端(应用程序——实用工具),运行: sudo vi /etc/hosts,此时屏幕上会提示你输入密码 打开 hosts 文件之后按 i 键进入插入模式(可理解为编辑 ...

  3. vue&webpack多页面配置

    前言 最近由于项目需求,选择使用vue框架,webpack打包直接使用的vue-cli,因为需要多页面而vue-cli只有单页面,所以就决定修改vue-cli的配置文件来满足开发需求. html-we ...

  4. django返回状态码的页面,指定p地址访问

    只有DEBUG = False 才能展示自定义的错误页面,否则djnago会报错,给出提示信息.  ps:django  DEBUG=FALSE, 会导致不能加载静态文件 ALLOWED_HOSTS ...

  5. uva-705-深搜

    题意,就是根据斜线组成的迷宫,判断能够组成多少个闭环. 解法: 放大俩倍或者三倍 俩倍 \     ------->10 01 三倍 \ ------->100 010 001 然后深搜, ...

  6. rhel7配置ELK过程

    参考网站:https://www.cnblogs.com/hongdada/p/7887455.html https://my.oschina.net/codingcloud/blog/1615013 ...

  7. 开发者必看|Android 8.0 新特性及开发指南

    背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...

  8. angular controller 之间的通信方式

    AngularJS中的controller是个函数,用来向视图的作用域($scope)添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在创建新的控制器时,angularJ ...

  9. jsp 调用其他jsp页面 跳转

    response.sendRedirect("test2.jsp"); window.location.reload("test2.jsp"); locatio ...

  10. mysql闯关练习

    1.表关系                 班级表:class       学生表:student       cid caption grade_id   sid sname gender clas ...