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 ...
随机推荐
- express 3.5 Err: request aborted
在处理app传过来的图片时遇到的,顾名思义,就是请求中断,图片在传输过程中遇到了网络不良问题,express 3.5 的中间件 bodyParser会在我们操作这些图片之前接收它们,接收过程中传输中断 ...
- python入门-异常
1 报错的例子 print(5/0) 2跳过报错的例子 try: print(5/0) except ZeroDivisionError: print("You can't divide b ...
- bean-json-bean-json 工具
package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExc ...
- ImageIO(图像处理)
1.通过ImageIO的read和writer,对图像文件进行处理. BufferedImage buffImage = ImageIO.read(file); // 将图像输出到Servlet输出流 ...
- eclipse 创建注释模板
使用 Alt+Shift+J 可以快速注释. 我们每次手动敲入作者,时间,版本等信息,有一些重复,可通过设置eclipse注释模板,减少工作量. Window -> preference -& ...
- Datatable数据分组
datatable里面的数据是按照这个顺序排列的 姓名 性别 年龄 a1 男 12 a1 女 11 a ...
- 使用JS获取当前地理位置方法汇总
使用JS获取当前地理位置方法汇总 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2014-12-18我要评论 这篇文章主要介绍了使用JS获取当前地理位置方法汇总,需要的朋友可以参考下 ...
- ubuntu16.04设置电池充电阈值
thinkpad在安装ubuntu16.04之后,设置充电阈值: 方法一: 使用双系统,在windows下使用联想的Lenovo setting center设置之后,在ubuntu之下也可以保持相同 ...
- React Native指南汇集了各类react-native学习资源、开源App和组件
来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...
- 代码报错记录-MAVEN
报错: COMPILATION ERROR : 程序包不存在. 说是找不到程序包,我的JUNIT是父项目中的,子项目是从JAVA项目转为MAVEN项目的,难道在转成MAVEN项目时对POM文件的修改有 ...