题目链接:

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
3AaA

output
2

input
7bcAAcbc

output
3

input
6aaBCCe

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.

这次没花过长时间看出尺取法来做,在尺取法的变形上多想了想。

首先预处理出总共有多少类型。

然后就普通的尺取法了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn = 1e5+5;
 9 char s[maxn];
10 map<char,int> mp,mp1;
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     scanf("%s",s+1);
16     int type = 0;
17     for(int i=1;i<=n;i++)
18     {
19         if(!mp.count(s[i]))
20         {
21             mp1[s[i]] = 1;
22             type++;
23             mp[s[i]] = 0;
24         }
25     }
26     int sum = 0;
27     int t = 1;
28     int ans = 1e9+7;
29     for(int l=1;l<=n;l++)
30     {
31         while(t<=n&&sum<type)
32         {
33             if(mp[s[t]]==0)
34             {
35                 sum++;
36             }
37             mp[s[t]]++;
38             t++;
39         }
40         if(sum<type) break;
41         ans = min(ans,t-l);
42         mp[s[l]]--;
43         if(mp[s[l]]==0)
44         {
45             sum--;
46         }
47     }
48     printf("%d\n",ans);
49     return 0;
50 }

转载于:https://www.cnblogs.com/littlepear/p/5815930.html

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

  1. 【推导】Codeforces Round #364 (Div. 2) D. As Fast As Possible

    一种方法是二分总时间,复杂度O(nlogn). 另外我们可以证明,当所有人同时到达终点的时候,是最优的,因为没有人的时间"浪费"了. 我们又发现,每个人的运动过程总是两段,要么是走 ...

  2. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  3. Codeforces Round #604 (Div.2)

    Codeforces Round #604 (Div.2) 2019/12/5 22:35--2019/12/6 00:35 Codeforces Round #604 (Div.2) A. Beau ...

  4. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  5. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  6. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  7. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  8. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  9. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

最新文章

  1. PyTorch中torchvision介绍
  2. Meta AI 新研究,统一模态的自监督新里程碑
  3. dos2unix批量转换的脚本
  4. 容器日志采集利器Log-Pilot
  5. readyboost提升明显吗_iphone12promax参数对比11ProMax区别 性能提升多少
  6. MaxCompute 挑战使用SQL进行序列数据处理
  7. LDAP命令介绍---dsreplication--enable:DISABLE
  8. opencv 读取、显示、保存视频
  9. 加载PageOffice控件失败。 当前浏览器是42版本以上的谷歌浏览器,建议采用POBrowser技术打开PageOffice即可。
  10. 如何将 Java 项目转换成 Maven 项目
  11. ectouch后台添加菜单
  12. js 视频插件zyMedia下载和使用方法
  13. 仿拼多多砍价功能玩法解说
  14. 全国各地省市地区plist文件(数据跟微信的地区一致)
  15. DCB改正——关于spp
  16. 5个海盗分100颗宝石
  17. 操作系统实验ucore_lab5实验报告
  18. 实例010 猴子吃桃
  19. pip加速源之python,快如火箭
  20. AMBA协议笔记(APB)

热门文章

  1. jsp常用的onchange事件
  2. 分布式事务的理解和解决方法
  3. html checked属性值,HTML复选框的checked属性的值是多少?
  4. Golang——秒懂函数、参数、可变参数、匿名函数、回调函数、内置函数
  5. linux系统无root权限lua库安装,liunx系统中安装lua以及torch
  6. 微信小程序定义全局变量_微信小程序第二天学习内容分享
  7. ElasticSearch 复合查询
  8. CDH5.16.2下载安装
  9. MySql通过Limit限制查询的行数
  10. 一本可能引发社会调查行业革命的书