Codeforces Round #364 (Div. 2) C 二分处理+求区间不同字符的个数 尺取法
2 seconds
256 megabytes
standard input
standard output
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.
题意:长度为n的字符串a 求最短的连续的子串 使得子串中出现所有在a串中出现过的字符
输出子串的长度。
题解:标记记录a串中字符的种类为zong种 下界为zong 上界为n 二分check答案
刚开始是想二维前缀和 处理任意区间的不同字符的个数 但是必然超时 cdoj有一题 可以水过
这里有一个处理的技巧 先预处理第一段长度为xx的字符串中不同字符的个数zha,之后动态的一位一位向右
移动 ,只考虑新增的一位字符,和舍弃的一位字符对 当前长度为xx的区间的影响 map处理
//code by drizzle
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
map<char,int> mp;
map<char,int> gg;
char a[];
int zong;
bool check(int xx)
{
int zha=;
gg.clear();
for(int i=;i<=xx;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
}
if(zha>=zong)
return true;
for(int i=xx+;i<=n;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
gg[a[i-xx]]--;
if(gg[a[i-xx]]==)
zha--;
if(zha>=zong)
return true;
}
return false;
}
int main()
{
zong=;
scanf("%d",&n);
getchar();
mp.clear();
for(int i=;i<=n;i++)
{
scanf("%c",&a[i]);
if(mp[a[i]]==)
{
mp[a[i]]=;
zong++;
}
}
int l=zong,r=n,mid=;
while(l<r)
{
mid=(l+r)/;
if(check(mid))
r=mid;
else
l=mid+;
}
printf("%d\n",l);
return ;
}
最新文章
- swiper框架,实现轮播图等滑动效果
- 单点登录实现----CAS(一)
- java使用jsch连接linux
- Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力)
- logback配置错误邮件发送
- win平台,apache通过web访问svn
- CentOS 6.4安装AMH面板
- 批量生成sqlldr文件,高速卸载数据
- C++中的namespace用法
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
- delegate-使用笔记
- jquery左右自适应伸缩插件
- linux文件、目录
- mac配置java和maven环境变量
- Linux系统根目录各文件夹的含义
- 搭建ubuntu14.04服务器必备环境
- Linux基础知识回顾及BASH学习
- 译: 2. RabbitMQ Spring AMQP 之 Work Queues
- 【Python】脚本运行报错:IndentationError: unindent does not match any outer indentation level
- NLTK 知识整理