Codeforces 701C. They Are Everywhere 思路题
2 seconds
standard input
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.
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.
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.
3
AaA
2
7
bcAAcbc
3
6
aaBCCe
5
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 思路题的更多相关文章
- CodeForces 606C--Sorting Railway Cars,思路题~~~
C - Sorting Railway Cars Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- 51nod P1305 Pairwise Sum and Divide ——思路题
久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...
- http://codeforces.com/gym/100623/attachments E题
http://codeforces.com/gym/100623/attachments E题第一个优化它虽然是镜像对称,但它毕竟是一一对称的,所以可以匹配串和模式串都从头到尾颠倒一下第二个优化,与次 ...
- POJ 1904 思路题
思路: 思路题 题目诡异地给了一组可行匹配 肯定有用啊-. 就把那组可行的解 女向男连一条有向边 如果男喜欢女 男向女连一条有向边 跑一边Tarjan就行了 (这个时候 环里的都能选 "增广 ...
- BZOJ 3252: 攻略(思路题)
传送门 解题思路 比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次..其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的 ...
- BZOJ 1303: [CQOI2009]中位数图(思路题)
传送门 解题思路 比较好想的思路题.首先肯定要把原序列转化一下,大于\(k\)的变成\(1\),小于\(k\)的变成\(-1\),然后求一个前缀和,还要用\(cnt[]\)记录一下前缀和每个数出现了几 ...
- http://codeforces.com/gym/100623/attachments H题
http://codeforces.com/gym/100623/attachments H题已经给出来的,包括后来添加的,都累加得到ans,那么从1-ans都是可以凑出来的,如果ans<a[n ...
随机推荐
- 经典算法 BFPRT算法详解
内容: 1.原始问题 => O(N*logN) 2.BFPRT算法 => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...
- stdio.h头文件中申明的基本函数
调用scanf函数时,需传入变量的地址作为参数,scanf函数会等待标准输入设备(键盘等)输入数据,并且将输入的数据赋值给地址对应的变量. #include<stdio.h> #inclu ...
- Bootstrap Table 中文文档(完整翻译版)
表格参数: 名称 标签 类型 默认 描述 - data-toggle String ‘table’ 不用写 JavaScript 直接启用表格. classes data-classes String ...
- bootStrap的小知识
代码模块 <code> 内联代码 <kbd> 用户输入 <pre>代码段 <var>数学字符 <samp>程序输出 表格 <thead ...
- java 项目中类找不到异常解决办法
最后点击Apply and Close就可以了
- 用jconsole监视内存使用情况
最近做性能压测,学习到可以用jconsole查看内存使用(连接端口:JMX_PORT=8060). 打开后发现,老年代内存一直无法释放,应该是应用启动参数中,老年代内存分配不够.加大内存,得到缓解:- ...
- 基于二进制RPC协议法的轻量级远程调用框架 ---- Hessian
使用Java创建Hessian服务有四个步骤: 1.创建Java接口作为公共API (client和server端 创建一个相同的借口) 2.使 ...
- MySQL优化order by导致的 using filesort
using filesort 一般出现在 使用了 order by 语句当中. using filesort不一定引起mysql的性能问题.但是如果查询次数非常多,那么每次在mysql中进行排序,还是 ...
- as3 加载库声音报错
排除法:(依次排序,从简单到难) 1.引用的声音类名与声音链接名字是否一致,可trace声音对象字符串检验 2.引用的声音对象是否不存在 ,可trace声音对象检验 3.最后检验是否当前swf中,其中 ...
- position_css
position: 定位,元素的定位与这五个属性相关.left,top,bottom,right,z-index 1. static (默认值).没有定位,五个属性都不起作用. 2. inherit ...