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 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<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+,M=1e6+,mod=1e9+,inf=1e9+;
int flag[];
int getnum(char a)
{
if(a>='A'&&a<='Z')
return a-'A';
return a-'a'+;
}
char a[N];
int main()
{
int x,y,z,i,t;
int cnt=;
scanf("%d%s",&x,&a);
for(i=;i<x;i++)
{
if(flag[getnum(a[i])]==)
cnt++;
flag[getnum(a[i])]++;
}
memset(flag,,sizeof(flag));
int st=,en=,gg=;
int ans=inf;
while()
{
while(en<x&&gg<cnt)
{
if(flag[getnum(a[en])]==)
gg++;
flag[getnum(a[en])]++;
en++;
}
if(gg<cnt)break;
ans=min(en-st,ans);
flag[getnum(a[st])]--;
if(flag[getnum(a[st])]==)
gg--;
st++;
}
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) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- 树形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 ...
随机推荐
- 爬虫实战【9】Selenium解析淘宝宝贝-获取宝贝信息并保存
通过昨天的分析,我们已经能到依次打开多个页面了,接下来就是获取每个页面上宝贝的信息了. 分析页面宝贝信息 [插入图片,宝贝信息各项内容] 从图片上看,每个宝贝有如下信息:price,title,url ...
- delphi 一些知识文章地址记录(正则)
正则运用:http://www.cnblogs.com/del/archive/2007/12/21/1008108.html
- git --fast-version-control
--distributed-is-the-new-centralized 读二进制文件 python 读doc xls 几乎所有的版本控制系统都以某种形式支持分支.使用分支意味着你可以把你的工作从 ...
- ArcPy开发IDE
最近时不时的使用Arcpy的脚本,这就不可避免的面临Python IDE的选择.可以用来Python开发的IDE非常的多,像Eclipse等等.尝试了一些IDE后,逐渐的固定使用下面两款. 一.Pyt ...
- 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线
最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...
- 快压、360压缩、WinRAR关于打开快压通过超高压缩比压缩后的文件不兼容的问题
今天接收了同事发过来的一个压缩文件,用360压缩打开和用WinRAR打开压缩文件,傻眼了,这发的是什么鬼压缩包.压缩包的文件大小有27533KB,用360压缩工具解压查看只有121.5kb,而且完全没 ...
- Flask(5)- Flask-Session组件、WTForms组件、数据库连接池(POOL)
一.Flask-Session 我们使用过flask内置的session,知道它是把session存放在浏览器,即客户端.今天要学习的flask-session是flask的第三方组件,看一下它和fl ...
- php socket 处理只是来数据流,该怎样避免(好像是堵塞了)
php socket 处理只是来数据流,该怎样处理(好像是堵塞了) 需求:php接受一个硬件往8888port上发送数据,假设收到后,应socket_send函数返回"\xFA\x01\x0 ...
- Python3+Selenium3自动化测试-(四)
selenium鼠标事件 # coding=utf-8 import time from selenium import webdriver from selenium.webdriver.commo ...
- (4.6)sql2008中的group by grouping sets
最近遇到一个情况,需要在内网系统中出一个统计报表.需要根据不同条件使用多个group by语句.需要将所有聚合的数据进行UNION操作来完成不同维度的统计查看. 直到发现在SQL SERVER 200 ...