codeforces #364c They Are Everywhere 尺取法
2 seconds
256 megabytes
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 number n - 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..
读题,就是一道尺取法的题,取最短的尺。
#include<iostream>
#include<map>
#include<set>
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstring>
using namespace std;
const int maxx = ;
int main(){
int n;
scanf("%d",&n);
char s[maxx];
scanf("%s",s);
set<int>pok;
int poks[maxx];
map<int,int>have;
for(int i=;i<n;i++){
poks[i]=s[i]-'A';
pok.insert(poks[i]);
}
int kind=pok.size();
int sta=;
int end=;
int num=;
int ans=0x7fffffff;
while(){
while(end<n&&num<kind){
if(have[poks[end]]==){
num++;
have[poks[end]]++;
end++;
}else{
have[poks[end]]++;
end++;
}
}
if(num<kind) break; ans=min(ans,end-sta);
have[poks[sta]]--;
if(have[poks[sta]]==) num--;
sta++; }
printf("%d\n",ans);
return ;
}
codeforces #364c They Are Everywhere 尺取法的更多相关文章
- CodeForces 701C They Are Everywhere 尺取法
简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 650B Image Preview(尺取法)
题目大概说手机有n张照片.通过左滑或者右滑循环切换照片,滑动需要花费a时间:看一张照片要1时间,而看过的可以马上跳过不用花时间,没看过的不能跳过:有些照片要横着看,要花b时间旋转方向.那么问T时间下最 ...
- Codeforces 660C Hard Process(尺取法)
题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...
- Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法
D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
随机推荐
- 洛谷——P1403 [AHOI2005]约数研究
P1403 [AHOI2005]约数研究 题目描述 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机“Samuel II”的长时间运算成为了可能.由于在去年一年的辛苦工 ...
- Vue之$set使用
背景 后端参与前端开发的小白,在开发过程中遇到了如下情况:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,如果更新此属性的值,是不会更新视图的. ...
- "library not found for - "解决办法
在我们编译的时候有时候会报这个错误 "library not found for - " 由于是我们在项目中使用了一些第三方的库,就比如我再使用百度的静态库文件的时候,报出的这个错 ...
- apache 单独生成模块
apache 单独生成模块 一般这种模块都是后期自己去生成的,比如一般在安装apache时都会--enable-so ,允许动态加载模块. 这个模块你可以这样去生成. 1.下载一个与当前使用的apa ...
- Word中更新交叉引用
方法一:选择要更新的域,按F9键即可. 方法二:右键单击要更新的域,在弹出的右键菜单中选择“更新域”即可. 方法三:若域位于一个含有“更新”按钮的特定容器中,则点击“更新”即可.
- lrc 校验码 ascii 格式
lrc 校验码 ascii 格式 将adr1 (站号)至最后一个数据内容相加,得到结果以256为单位,超出部分去除(如得到的结果为16#128H则只取28H,) 然后计算二次反补得到后的结果即为侦误 ...
- Python中使用XMLRPC(入门)
一.简介 RPC是Remote Procedure Call的缩写,翻译成中文为:远程方法调用. 它是一种在本地机器上调用远端机器上的一个过程(方法)的技术,这个过程也被大家称为“分布式计算”,是为了 ...
- SyBase 百科
ylbtech_database_sybase 1, 百度百科 http://baike.baidu.com/view/118488.htm?fr=aladdin
- linux 下查看硬件信息(mac,IP地址,硬盘型号,序列号等)
一.查看网卡mac地址 #安装lshw [root@server ~]# yum install lshw #使用方法 [root@rsync-server ~]# lshw -c network * ...
- LLVM每日谈之二十 Everything && Clang driver
作者:史宁宁(snsn1984) 近期在读<Getting Started with LLVM Core Libraries>.这是读的第一本LLVM的书.非常多地方尽管讲的是自己知道的东 ...