B - They Are Everywhere CodeForces - 701C
题目链接 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.
题意:就是要抓小精灵,在每个房间有着未知的精灵,(1~n),然后你想抓到这个街区所有的不同的小精灵,但是你又比较懒,所以你想要去最短的房间数,然后抓到所有的不同的小精灵。
思路:因为小精灵可能一样,所以用到map标记,然后就尺取法,跟 例子2一样的道理 算是练习了!!!一发AC
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
map<int,int> st;
int a[100001];
int main(){
int n;
cin>>n;
char c;
set<int> s;
for(int i=0;i<n;i++){
cin>>c;
a[i] = c-'A'+1;
s.insert(a[i]);
}
int cnt = s.size();//确定小精灵的数量
s.clear();
int s1 = 0,t=0,sum=0;
int res = inf;
while(1){
while(sum < cnt && t < n){
if(st[a[t++]]++==0){
sum++;
}
}
if(sum<cnt) break;
res = min(res,t-s1);
if(--st[a[s1++]]==0){
sum--;
}
}
cout<<res<<endl;
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用卷积神经网络构建图像分类模型检测肺炎
- 如何提升docker容器安全性
- CICD(三)Ansible常用模块以及案例
- 深入SVM:支持向量机核的作用是什么
- kubernetes(三)之Docker网络详解
- kubernetes(二)之Docker容器及镜像
- 反向传播算法:定义,概念,可视化
- Go语言(二十一) 常见的模块使用
- kubernetes(一)之Docker基础入门
- Go语言(二十)日志采集项目(二)Etcd的使用
- prometheus入门(一)
- Go语言(十九)日志采集项目之logagent开发(一)
- Go语言(十 八)context&日志项目
- 使用梯度上升欺骗神经网络,让网络进行错误的分类
- Go语言(十七) 配置文件库项目