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不停++,然后如果遇到需要改变 ...
随机推荐
- codevs 3961 硬币找零【完全背包DP/记忆化搜索】
题目描述 Description 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从银行拿回最少的硬币数,并保证能用这些硬币发工资. 我们应该 ...
- Linux是32位还是64位
命令行输入 file /bin/ls 显示 /sbin/init: ELF 64-bit LSB executable, x86-64 ... 则为64位 file /sbin/init /sbi ...
- bzoj 3462: DZY Loves Math II
3462: DZY Loves Math II Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 211 Solved: 103[Submit][Sta ...
- SQL SERVER Update from 使用陷阱
原文:SQL SERVER Update from 使用陷阱 update A set from A left join B on 此方法常用来使用根据一个表更新另一个表的数据,来进行数据同步更新.若 ...
- ubifs & mtd
前天晚上在写完另一篇总结之时,赵XX向我咨询了关于mtd 和ubifs的相关内容.而我在这方面只是略懂皮毛,所以向他许愿共同调查这个方面的知识.经过昨天一天的调查,最后感觉是有了一定的经验和基础了,所 ...
- maven项目中的GroupId和ArtifactId分别是什么含义
转自 创建maven项目是其中的group id和artifact id怎么填写 groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓 ...
- Features (OCMock 2)
This page describes the features present in OCMock 2.x, using the traditional syntax. All these feat ...
- C# HttpWebRequest 绝技 【转】
原文地址:http://www.sufeinet.com/thread-6-1-1.html 在线测试工具http://www.sufeinet.com/thread-3690-1-1.html c# ...
- oc的插件
umbra https://umbra3d.com/ 很不错
- 一个例子理解Predicate、Consumer和Stream
一个需求: 把年龄大于20的学生的信息打印出来. 面向对象编程 public class Student { private String name; private int age; private ...