cf701C They Are Everywhere
2016-07-23 12:58
281 查看
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
output
input
output
input
output
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.
这种sb题卡了我好久我真是。。。
题意给定一个10w的串,只包含英文字母且区分大小写,求 包含所有出现的字母的,长度最小的子串的长度。
我怎么一开始看到都没想到二分啊。。
显然二分+判定是可行的,知道长度的话开个52的数组然后扫过去,每次判定这一段是否满足条件。
虽然算法时间上限是nlogn*52,但是实际运行是远远达不到这个上限的
cf701C
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.
这种sb题卡了我好久我真是。。。
题意给定一个10w的串,只包含英文字母且区分大小写,求 包含所有出现的字母的,长度最小的子串的长度。
我怎么一开始看到都没想到二分啊。。
显然二分+判定是可行的,知道长度的话开个52的数组然后扫过去,每次判定这一段是否满足条件。
虽然算法时间上限是nlogn*52,但是实际运行是远远达不到这个上限的
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<deque> #include<set> #include<map> #include<ctime> #define LL long long #define inf 0x7ffffff #define pa pair<int,int> #define pi 3.1415926535897932384626433832795028841971 using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void write(LL a) { if (a<0){printf("-");a=-a;} if (a>=10)write(a/10); putchar(a%10+'0'); } inline void writeln(LL a){write(a);printf("\n");} int n,l,r,ans,mrk2[200]; int s[100010]; char ch[100010]; bool mrk[200]; inline bool jud(int x) { memset(mrk2,0,sizeof(mrk2)); for (int i=1;i<=n;i++) { mrk2[ch[i]+0]++; if (i>x)mrk2[ch[i-x]+0]--; bool mk=0; for (int i=1;i<200;i++) if (mrk[i]) if (!mrk2[i]){mk=1;break;} if (!mk)return 1; } return 0; } int main() { n=read(); scanf("%s",ch+1); for(int i=1;i<=n;i++) { mrk[ch[i]+0]=1; } l=1;r=n; while (l<=r) { int mid=(l+r)>>1; if (jud(mid)){ans=mid;r=mid-1;} else l=mid+1; } printf("%d\n",ans); }
cf701C
相关文章推荐
- centos7.2安装配置
- View的事件分发机制理解
- HDU 5726 GCD(枚举暴力)
- NAT原理?代理服务器原理?
- 树状数组模版(单点修改区间求和)(区间修改单点求值)(区间修改区间求和)
- Android 封装标题栏
- 【VS开发】动态添加的ActiveX控件如何响应事件
- vector::capacity
- Python之定向爬虫Scrapy
- CentOS系统操作mysql的常用命令
- adb无线调试
- IO流读取键盘录入
- UVA12558->IDDFS
- LLDB之理解LLDB基本命令语法
- 5. Longest Palindromic Substring && 214. Shortest Palindrome && 336. Palindrome Pairs
- 【VS开发】windows注册ActiveX控件
- eclipse编写java时printf出现错误
- soft raid5阅读笔记之十--状态标志
- 剑指offer 34题 【时间空间效率的平衡】丑数
- 【VS开发】动态创建ActiveX控件