Codeforces Round #364 (Div. 2) C 尺取法

来源:互联网 发布:微软流程图软件 编辑:程序博客网 时间:2024/05/10 08:10



链接:戳这里


C. They Are Everywhere
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.


题意:

给出长度为n的字符串,找出最短的区间使得包含所有出现过的字符


思路:

尺取法:求满足条件的最小区间的方法

个人对尺取法的理解:假设当前区间[l,r]满足条件使得所有字符都在区间里。那么去掉开头的s[l]字符,剩下[l+1,r],判断是否满足条件。如果满足,更新答案。如果不满足,那么尾部继续向后拓展,判断[l+1,r+1]区间是否满足条件。重复这一操作就可以了。O(n)

本题思想基本差不多,但是我还是不知道比赛的时候我为什么会错。。。。


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;int n;string s;map<char ,int> mp;map<char ,int> mp1;int a[100100];int main(){    scanf("%d",&n);    cin>>s;    int num=0;    for(int i=0;i<n;i++){        if(mp[s[i]]==0) {            num++;        }        mp[s[i]]++;    }    int ans=n,cnt=0,l=0,r=0,t=0;    while(1){        while(r<n && cnt<num){            if(mp1[s[r]]==0){                cnt++;            }            mp1[s[r]]++;            r++;        }        if(cnt<num) break;        ans=min(ans,r-l);        mp1[s[l]]--;        if(mp1[s[l]]==0) cnt--;        l++;    }    cout<<ans<<endl;    return 0;}



1 0
原创粉丝点击