题目链接:

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
char s[maxn];
map<char,int> mp,mp1;
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s+);
int type = ;
for(int i=;i<=n;i++)
{
if(!mp.count(s[i]))
{
mp1[s[i]] = ;
type++;
mp[s[i]] = ;
}
}
int sum = ;
int t = ;
int ans = 1e9+;
for(int l=;l<=n;l++)
{
while(t<=n&&sum<type)
{
if(mp[s[t]]==)
{
sum++;
}
mp[s[t]]++;
t++;
}
if(sum<type) break;
ans = min(ans,t-l);
mp[s[l]]--;
if(mp[s[l]]==)
{
sum--;
}
}
printf("%d\n",ans);
return ;
}

Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)的更多相关文章

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

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces Round #364 (Div. 2)

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

  3. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

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

  4. Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)

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

  5. 树形dp Codeforces Round #364 (Div. 1)B

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

  6. 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 ...

  7. Codeforces Round #364 (Div. 2) Cells Not Under Attack

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

  8. Codeforces Round #364 (Div. 2) Cards

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

  9. 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 ...

  10. 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 ...

随机推荐

  1. 矩阵快速幂AC代码HDU 2035

    #include <iostream> using namespace std;const int MOD = 1000;//像这样的一个常量就应该专门定义一下 int PowMod(in ...

  2. lldpd-0.7.7代码解读(send_pdu部分)

    此文档是经过逆序推到出的,可能有错误之处,敬请指教,谢谢. 1)interfaces_update 更新一些接口信息 2)levent_iface_subscribe 该接口通过socket通信(非阻 ...

  3. hdu 2503 a/b + c/d

    Problem Description 给你2个分数,求他们的和,并要求和为最简形式.   Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四 ...

  4. python2.6.6在centos6.4下安装

    1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...

  5. 用 js 做 URL 跳转带来的 Referer 丢失问题.

    http 302 重定向是可以保持 referer 的.例:在 A 页面上提交登录表单到 B,B 返回一个重定向页面到 C,在 C 处理里面检查 Referer 可知道它的来源是 A 而不是 B. 但 ...

  6. MySQL-测试卷一

    MySQL-测试卷一 一.单项选择题 1 下面不属于Msql数据库特点的是(  ) A. 免费使用  B.不能跨平台  C.开源软件  D.功能强大 2 定义表的一个字段, 要求能表示4位整数,2位小 ...

  7. linux跨主机复制文件或文件夹

    复制文件基本格式:(本地到远程) scp 文件名 用户名@ip:文件全目录 如果是文件夹加上参数 -r scp -r 基础目录 用户名@ip:目录 栗子: scp local_file remote_ ...

  8. 使用jstl标签遍历双层的map(map下面的map)

    <c:forEach var="firstMap" items="${map}"> <c:forEach var="secondMa ...

  9. WPF从我炫系列4---装饰控件的用法

    这一节的讲解中,我将为大家介绍WPF装饰控件的用法,主要为大家讲解一下几个控件的用法. ScrollViewer滚动条控件 Border边框控件 ViewBox自由缩放控件 1. ScrollView ...

  10. Android.mk与jni目录的关系

    附1: Android.mk与jni目录的关系: 在某目录下,如/src/modules1/下存放Android.mk和Application.mk,调用ndk-build试图编译时,会遇到如下错误: ...