Codeforces Round #364 (Div. 2) C. They Are Everywhere 尺取法

时间:2023-02-14 16:52:30
C. They Are Everywhere
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3
AaA
output
2
input
7
bcAAcbc
output
3
input
6
aaBCCe
output
5
Note

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 尺取法的更多相关文章

  1. Codeforces Round &num;364 &lpar;Div&period; 2&rpar;

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  2. Codeforces Round &num;364 &lpar;Div&period;2&rpar; C&colon;They Are Everywhere(双指针&sol;尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  3. Codeforces Round &num;364 &lpar;Div&period;2&rpar; D&colon;As Fast As Possible(模拟&plus;推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  4. 树形dp Codeforces Round &num;364 &lpar;Div&period; 1&rpar;B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  5. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; B&period; Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  7. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; Cards

    Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...

  8. Codeforces Round &num;364 &lpar;Div&period; 2&rpar;-&gt&semi;A&period; Cards

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. Codeforces Round &num;364 &lpar;Div&period; 2&rpar; E&period; Connecting Universities

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. Python之路Day16--JavaScript&lpar;二&rpar;

    本节内容: 1.上节内容回顾 2.JavaScript补充 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 一.上节内容回顾 1.作业问题: a.页面布局不好 ...

  2. 【solr】java整合solr5&period;0之solrj的使用

    1.首先导入solrj需要的的架包 2.需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrClient替代这个类了,在添加之后首先我们需要根据schem ...

  3. Appium 小白从零安装 ,Appium连接真机测试。

    以下是我个人在初次安装使用Appium时的过程,过程中遇到了一些问题,在这里也一一给出解决办法. Appium安装过程 先安装了 Node.js.在node的官网上下载的exe安装文件. 在node的 ...

  4. Colletion View 简单的备忘

    UIColletionView 这篇只是做UIColletionView的常用属性.代理方法和数据源方法的备忘,之后做一些自定义布局,增加删除动画等. UIColletionViewFlowLayou ...

  5. hibernate&lowbar;validator&lowbar;04

    对象图--个人觉得就是关联验证 ean Validation API不仅能够用来校验单个的实例对象,还能够用来校验完整的对象图.要使用这个功能,只需要在一个有关联关系的字段或者属性上标注 @Valid ...

  6. 填涂颜色 洛谷 p1162

    题目描述 由数字0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向.现要求把闭合圈内的所有空间都填写成2.例如:6X6的方阵(n=6),涂色前和涂色后的方阵如下: 0 ...

  7. 洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解&plus;代码

    洛谷P2891 Dining P1402 酒店之王[类二分图匹配]题解+代码 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的 ...

  8. 相约南湖,南京都昌信息亮相南湖HIT论坛

    金秋十月,雨过南湖水似油 ,烟雾蒙蒙净长空 2017年10月15日, 南湖HIT论坛迎来了第六届.本次论坛吸引了500名来自全国各地医疗机构.卫生行政主管部门的信息化主管和医疗IT企业的精英,齐聚嘉兴 ...

  9. MySQL登录报错ERROR 2002 &lpar;HY000&rpar;&colon; Can&&num;39&semi;t connect to local MySQL server through socket &&num;39&semi;&sol;tmp&sol;mysql&period;sock&&num;39&semi; &lpar;2&rpar;

    [root@pisphkdcbsql01 mysql3307]# /opt/mysql3307/bin/mysql -upisp -ppisp@ mysql: [Warning] Using a pa ...

  10. 2D游戏新手引导点光源和类迷雾实现

    一.新手引导须要的遮罩效果 一般做新手引导的时候,会把游戏画面变的半黑,然后须要玩家点击的地方就亮起来.经常使用的做法是採用遮罩来实现,可是仅仅能实现方形的,不能不规则图形.以及是全然挖空.做不到渐变 ...