They Are Everywhere————二分+尺取练习

红太狼 2022-05-18 09:55 256阅读 0赞

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.


  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<map>
  5. #include<set>
  6. #include<algorithm>
  7. using namespace std;
  8. const int MAXN=1e5+9;
  9. int main()
  10. {
  11. int P;
  12. char str[MAXN];
  13. int a[MAXN];
  14. memset(str,0,sizeof(str));
  15. scanf("%d %s",&P,str);
  16. set<int> all;
  17. for(int i=0;i<P;i++)
  18. {
  19. a[i]=str[i]-'A'+1;
  20. all.insert(str[i]-'A'+1);
  21. }
  22. int n=all.size();
  23. int s=0,t=0,num=0;
  24. map<int,int> count;
  25. int ans=P;
  26. for(;;)
  27. {
  28. while(t<P && num<n)
  29. if(count[a[t++]]++ ==0)
  30. num++;
  31. if(num<n) break;
  32. ans=min(ans,t-s);
  33. if(--count[a[s++]]==0)
  34. num--;
  35. }
  36. printf("%d\n",ans);
  37. return 0;
  38. }

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 法总结

    一般地,当我们要去枚举满足条件的区间权值的区间,可以用遍历双指针 l 和 r 的做法去枚举第一个满足条件的区间,然后去维护其他数据,这样的做法就是尺取法,它的复杂度为O(n)

    相关 算法--

    尺取法 尺取法通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到得出答案的方法,这种操作很像是尺取虫爬行的方式故得名。 我们先来看看POJ的

    相关 Pie————二分+练习

    给你n个蛋糕的半径,你有m个朋友,所以总共有m+1个人,现在要分蛋糕,要求每个人分到的大小都是一样的,且每个人的蛋糕都是从一块上切割下来的(不能是2个不同的蛋糕拼起来的),现在