cf701C They Are Everywhere
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.
这种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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<){printf("-");a=-a;}
if (a>=)write(a/);
putchar(a%+'');
}
inline void writeln(LL a){write(a);printf("\n");}
int n,l,r,ans,mrk2[];
int s[];
char ch[];
bool mrk[];
inline bool jud(int x)
{
memset(mrk2,,sizeof(mrk2));
for (int i=;i<=n;i++)
{
mrk2[ch[i]+]++;
if (i>x)mrk2[ch[i-x]+]--;
bool mk=;
for (int i=;i<;i++)
if (mrk[i])
if (!mrk2[i]){mk=;break;}
if (!mk)return ;
}
return ;
}
int main()
{
n=read();
scanf("%s",ch+);
for(int i=;i<=n;i++)
{
mrk[ch[i]+]=;
}
l=;r=n;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid)){ans=mid;r=mid-;}
else l=mid+;
}
printf("%d\n",ans);
}
cf701C
cf701C They Are Everywhere的更多相关文章
随机推荐
- Linux access
1.access函数 功能描述:检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h>int ac ...
- noip 2015 运输计划 (lca+二分)
/* 95 最后一个点T了 qian lv ji qiong 了 没学过树剖 听chx听xzc说的神奇的方法 Orz 首先求出每个计划的路径长度 这里写的倍增 然后二分答案 对于每个ans 统计> ...
- codevs 1213 解的个数(我去年打了个表 - -)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int T,x ...
- 5阻止A默认行为和JS实现页面跳转的方法
<!--HTML中阻止A标签的默认行为: href="javascript:;" href="javascript:void 0;"--><! ...
- Web字体工具整理,网页图标字体以及使用方法整理
常用网站app logo 下载: http://www.iconfont.cn/collections/show/268?spm=a313x.7781069.0.0.nDYawz 1.Web字体免费生 ...
- iOS开发中EXC_BAD_ACCESS的另类原因
今天偶然学习iOS开发的时候碰到一个EXC_BAD_ACCESS的异常,经查资料得到的解释是由于访问了已经被回收了堆内存对象导致的,参考: http://code.tutsplus.com/tutor ...
- AlertView + Block 的使用
AlertView的使用还是很频繁的,扁平化以后似乎也不是那么丑了,但是到现在为止官方还是在用Delegate来处理点击事件,很影响程序的可读性. 如果用Block那么就会一目了然.自己本来想写一个, ...
- ACE的Socket初步
Tcp通信过程一般为如下步骤: 服务器绑定端口,等待客户端连接. 客户端通过服务器的ip和服务器绑定的端口连接服务器. 服务器和客户端通过网络建立一条数据通路,通过这条数据通路进行数据交互. 常用AP ...
- js 判断url的?后参数是否包含某个字符串
function GetQueryString(name){ var reg=eval("/"+name+"/g"); var r = window. ...
- Extjs之遍历Store内的数据
Store作为数据的载体,通过下面的方法可以获得Store内的数据; Ext.define('haomlGeimjTongjGrid_store_data', { extend: 'Ext.data. ...