B - They Are Everywhere CodeForces - 701C

2020-09-10 22:10:48 浏览数 (2)

题目链接

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

代码语言:javascript复制
#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;
}

0 人点赞