They Are Everywhere

链接:http://acm.hust.edu.cn/vjudge/problem/427892/origin

题目:

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.



题意:一个字符串,求一个最小的连续子序列的长度使其包含所有出现过的字符。

分析:首先要遍历一遍字符,记录一共出现过多少个字符,然后用尺取法,枚举尺取头和尾,不满足所有都出现过就尾巴++,满足就记录长度,头++直到不满足。循环到结尾。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int n;
char s[100010];
map<char,int> cur;
set<char> setc;

int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d",&n))
	{
		setc.clear();
		cur.clear();
		for(int p=0;p<n;p++)
		{
			cin>>s[p];
			setc.insert(s[p]);
		}
		int ans=999999999;
		int l=0,r=0;
		for(l=0;l<n;l++)
		{
			while(cur.size()!=setc.size()&&r<n)
				cur[s[r++]]++;
			while(l<r&&cur[s[l]]>1)
				cur[s[l++]]--;
			if(cur.size()==setc.size())
				ans=(ans<(r-l))?ans:(r-l);
			cur[s[l]]--;
			if(cur[s[l]]==0)
				cur.erase(s[l]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值