C. They is Everywhere time limit per test 2 seconds memory limit per test megabytes input standard input output stand ARD output
Sergei B., the young coach of Pokemons, have found the big house which consists of N flats ordered in a row from left to RI Ght. It is possible-enter each flat from the street. It is the possible to go off from each flat. Also, each flat was 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 are 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 a nd then go off 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 T Hat appear in the house. Your task is to help him and determine this minimum number of flats he have to visit. Input
The first line contains the integer n (1≤n≤100)-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 中文版 alphabet, The i-th letter equals the type of Pokemon, which was 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 is 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.
Source
Codeforces Round #364 (Div. 2)
My Solution
Minimum interval length for all kinds of elements
Pointers or binary search
1. Binary Search
int L = 0, r = N;
while (L + 1 < r) {
x = (L + r) >> 1;
if (check (x)) r = x; O (n) checks whether Sz = x satisfies the condition, if satisfied then R = x, and then continues to check for x smaller case, last ans = = x;
else L = x;
}
Complexity O (NLOGN)
2. Pointers
The number of types in the maintenance interval with SZ//if used each time. Size () seems to be O (n), so that it becomes O (1) with a sz.
Use M[flats[j]], m[flats[i]] to denote the number of elements in the interval [I, j],
When J moves to the right one and the original m[flats[j]] = = 0 o'clock sz++, m[flats[j]]++;
When I move to the right one and the original m[flats[i]] = = 1 o'clock sz--, m[flats[j]]--;
When Sz = = kinds, maintenance answer ans = min (ans, j-i);
Complexity O (N)
Source code for the pointers
#include <iostream> #include <cstdio> #include <map> using namespace std;
typedef long Long LL;
const int MAXN = 1e5 + 8;
Char FLATS[MAXN];
Map<char, int> m;
int main () {#ifdef LOCAL freopen ("A.txt", "R", stdin);
Freopen ("B.txt", "w", stdout);
int T = 3;
while (t--) {#endif//LOCAL int n, kinds, ans = maxn, sz;
scanf ("%d", &n);
scanf ("%s", flats);
for (int i = 0; i < n; i++) m[flats[i]]++;
kinds = m.size ();
M.clear (); sz = 1;
int i = 0, j = 1;
m[flats[0]]++;
while (true) {if (i = = j) break;
if (j = = N && sz! = kinds) break;
while (sz! = kinds) {if (j = = n) break;
if (m[flats[j]] = = 0) sz++;
m[flats[j]]++;
j + +;
} if (sz = = kinds) ans = min (ans, j-i);
if (m[flats[i]] = = 1) sz--;
m[flats[i]]--;
i++;
cout<<i<<endl;
} printf ("%d", ans); #ifdeF LOCAL printf ("\ n");
M.clear ();
} #endif//LOCAL return 0; }
Thank you!
------from Prolights