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.
3
AaA
2
7
bcAAcbc
3
6
aaBCCe
5
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的字符串a 求最短的连续的子串 使得子串中出现所有在a串中出现过的字符
输出子串的长度。
题解:标记记录a串中字符的种类为zong种 下界为zong 上界为n 二分check答案
刚开始是想二维前缀和 处理任意区间的不同字符的个数 但是必然超时 cdoj有一题 可以水过
这里有一个处理的技巧 先预处理第一段长度为xx的字符串中不同字符的个数zha,之后动态的一位一位向右
移动 ,只考虑新增的一位字符,和舍弃的一位字符对 当前长度为xx的区间的影响 map处理
1 //code by drizzle 2 #include<bits/stdc++.h> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #define ll __int64 7 #define PI acos(-1.0) 8 #define mod 1000000007 9 using namespace std; 10 int n; 11 map<char,int> mp; 12 map<char,int> gg; 13 char a[100005]; 14 int zong; 15 bool check(int xx) 16 { 17 int zha=0; 18 gg.clear(); 19 for(int i=1;i<=xx;i++) 20 { 21 if(gg[a[i]]==0) 22 zha++; 23 gg[a[i]]++; 24 } 25 if(zha>=zong) 26 return true; 27 for(int i=xx+1;i<=n;i++) 28 { 29 if(gg[a[i]]==0) 30 zha++; 31 gg[a[i]]++; 32 gg[a[i-xx]]--; 33 if(gg[a[i-xx]]==0) 34 zha--; 35 if(zha>=zong) 36 return true; 37 } 38 return false; 39 } 40 int main() 41 { 42 zong=0; 43 scanf("%d",&n); 44 getchar(); 45 mp.clear(); 46 for(int i=1;i<=n;i++) 47 { 48 scanf("%c",&a[i]); 49 if(mp[a[i]]==0) 50 { 51 mp[a[i]]=1; 52 zong++; 53 } 54 } 55 int l=zong,r=n,mid=0; 56 while(l<r) 57 { 58 mid=(l+r)/2; 59 if(check(mid)) 60 r=mid; 61 else 62 l=mid+1; 63 } 64 printf("%d ",l); 65 return 0; 66 }