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的更多相关文章
随机推荐
- android 安全退出应用程序的几种方法
android 安全退出应用程序的几种方法 正常关闭应用程序: 当应用不再使用时,通常需要关闭应用,可以使用以下三种方法关闭android应用: 第一种方法:首先获取当前进程的id,然后杀死该进程.a ...
- shijan
1.<?php 2. $zero1=date(“y-m-d h:i:s”); 3. $zero2=”2010-11-29 21:07:00′; 4. echo “zero1的时间为:”.$zer ...
- (转)如何将ecshop首页主广告位的flash轮播替换为js轮播
转之--http://www.ecshoptemplate.com/article-1710.html 这个ecshop很常见,因为现在比起flash难以修改,js更加符合人们的使用习惯,而默认ecs ...
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
- NOI 193棋盘分割.cpp
193:棋盘分割 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分 ...
- 解决Mac上Android开发时adb连接不到手机问题
今天在Mac OS上进行Android开发的时候,打开eclipse连接不到手机MX4问题 1. 插入手机打开 Terminal,输入 system_profiler SPUSBDataType 2 ...
- 正则表达式 U贪婪模式
<?php/*模式修正符号: i u 位置:"//模式修正符位置" 可以一次使用一个,也可以一次使用多个 对整个正则表达式调优用的,也可以说是对正则表达式功能的扩展 &quo ...
- jquery mobile navbar
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 原生JS 选项卡代码实现
可实现同页面多个选项卡 效果图: 代码实现: HTML部分 <div class="main" id="tabs"> <div class=& ...
- 初涉JavaScript模式 (5) : 原型模式 【一】
什么是原型模式? 原型模式(prototype)是指用原型实例指向创建对象的种类,并且通过拷贝这些原型创建新的对象.--引自JavaScript设计模式 我们创建的每一个函数都有一个prototype ...