Codeforces Round #364 (Div. 2) C.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 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.
题解:
很裸的双指针
#include<cstdio>
#include<cstring>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
char a[];
int vis[]; inline int getid(char x){
if(x>='A'&&x<='Z')return x-'A';
return x-'a'+;
}
int main(){
int n,all=;
scanf("%d",&n);
scanf("%s",a+);
F(i,,n)vis[getid(a[i])]=;
F(i,,)if(vis[i])all++;
memset(vis,,sizeof(vis));
int l=,r=,cnt=,ans=;
while(r<=n)
{
while(r<n&&cnt<all){
r++;
int idx=getid(a[r]);
if(vis[idx]==)cnt++;
vis[idx]++;
}
if(r==n&&cnt<all)break;
if(cnt==all){ans=min(ans,r-l+);}
while(cnt==all){
int idx=getid(a[l]);
vis[idx]--;
if(vis[idx]==)cnt--;
l++;
if(cnt==all)ans=min(ans,r-l+);
}
}
printf("%d\n",ans);
return ;
}
Codeforces Round #364 (Div. 2) C.They Are Everywhere的更多相关文章
- Codeforces Round #364 (Div. 2)
这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- Codeforces Round #364 (Div. 2) B. Cells Not Under Attack
B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #364 (Div. 2) Cells Not Under Attack
Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...
- Codeforces Round #364 (Div. 2) Cards
Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...
- Codeforces Round #364 (Div. 2)->A. Cards
A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #364 (Div. 2) E. Connecting Universities
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- open()的模块
对文件操作流程: 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 mode can be: * 'r' 只读. * 'w' 写入,如果之前有就覆盖 * 'a' ...
- openstack私有云布署实践【6 RabbitMQ】
生产环境建议在集群三台controller上做rabbitmq 使用到队列的openstack组件 OpenStack Compute OpenStack Block Storage OpenStac ...
- linux版本选择
桌面系统 -- Ubuntu,开发人员也喜欢用Ubuntu 服务器端 -- RHEL或CentOS,RHEL要钱 对安全要求很高 -- Decian或Free ...
- 【1】ubuntu 安装docker
官方支持安装docker的Ubuntu版本: ubuntu trusty 14.04(LTS) (64位) ubuntu precise 12.04(LTS) (64位) ubuntu raring ...
- iOS开发上架之itunes connect里app信息的编辑
sku用于我们在后台识别自己的app,所以随你怎么填写
- 把嵌入在eclipse中的tomcat日志分离出来
现象 不知道从哪个版本的tomcat开始,windows版本的tomcat不再包含{tomcat_home}\logs\catalina.out这个文件,eclipse中配置好tomcat服务器之后, ...
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- 小心DLL链接静态库时的内存错误
本文转自http://www.bennychen.cn/2010/09/%E5%B0%8F%E5%BF%83dll%E9%93%BE%E6%8E%A5%E9%9D%99%E6%80%81%E5%BA% ...
- Chapter 14_4 使用_ENV
因为_ENV是一个普通的变量,我们可以像其他变量一样去对它进行赋值和访问. _ENV = nil 上面的赋值操作,将会使得在它之后的代码块不能直接访问全局变量.不过,对控制你的代码所使用的变量有用处. ...
- ejabberd聊天室离线消息
首先,xmpp服务器是基于ejabberd.离线消息模块是mod_interact,原地址地址:https://github.com /adamvduke/mod_interact: 修改后实现群聊离 ...