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的更多相关文章
随机推荐
- 非空验证(源代码Java版)
import java.util.Map; /** * 非空验证工具类 */ public class UntilEmpty { /** * @see: 验证string类型的是否为空 */ publ ...
- codevs1404字符串匹配
/* 无奈我改了那么久还是看的题解 首先跑一边kmp 几下ans[p]表示总共匹配到长度p的次数 这些不一定都是恰好到p 所以在处理一下 ans[p]通过处理变成 所有的匹配到长度p的次数 最后答案就 ...
- Web api 文档以及测试工具配置
第一步: 创建web api 在nuget 上搜索 webapitestclient (包含预发行版) 然后在 /Areas/HelpPage/Views/Help/Api.cshtml 末尾 添加 ...
- (一)SAPI简述
SAPI,软件中的语音技术包括两方面的内容,一个是语音识别(speech recognition) 和语音合成(speech synthesis).这两个技术都需要语音引擎的支持. 下面我们来了解下基 ...
- excel - 相等判断
函数使用例子: =IF(EVALUATE(B23)=C23,"正确","错误") 即判断单元格'B23'与'C23'是否相等,若为true,则返回"正 ...
- windows下安装CI框架
CI框架是一个非常流行的 mvc框架, CI框架如何安装和使用,在CI中文网已经讲的比较详细了 ,这里记录下几个需要注意的地方. 一. index.php问题 把压缩包下载解压到项目根目录即可运行里面 ...
- JavaScript--赋值表达式(typeof-delete-void)
typeof运算符 typeof是一个一元运算符,操作数可以使任意类型,返回值为操作数类型的一个字符串 一.数字类型,如typeof(1),返回的值就是number.当然这个是常规数字,对于非常规的数 ...
- hibernate_validator_06
认证组(校验组) 校验组能够让你在验证的时候选择应用哪些约束条件. 这样在某些情况下( 例如向导 ) 就可以对每一步进行校验的时候, 选取对应这步的那些约束条件进行验证了. 校验组是通过可变参数传递给 ...
- thinkphp 邮件发送
最近项目上要求,要做个邮件发送的功能,因为用到的框架是ThinkPHP,于是就自己整理一下. 引入class.phpmailer.php,大家可以去这个链接去下载: http://pan.baidu. ...
- QT实现单个EXE文件
有时候发布用Qt写的软件是件令人烦恼的事情,明明发布的只是一个简单功能的小软件,非得再附上一堆超大的动态链接库,实在让人觉得汗颜 . 在可执行文件单文件化方面,有多种方法.常用的是编译并使用静态 Qt ...