CF 701C They Are Everywhere(尺取法)
题目链接: 传送门
They Are Everywhere
time limit per test:2 second memory limit per test:256 megabytes
Description
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.
Sample Input
3
AaA
7
bcAAcbc
6
aaBCCe
Sample Output
2
3
5
解题思路:
题目大意:给一串字符串,问包含字符串中所有字符种类的区间最小长度。
类型题POJ 3320
假设从某一位置,为了覆盖所有种类字符串需要到t位置,这样的话可以知道如果从s+1开始开始的话,那么必须到t'位置为止。由此可以考虑尺取法。
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int N;
char str[100005];
set<char>all;
memset(str,0,sizeof(str));
scanf("%d",&N);
scanf("%s",str);
for (int i = 0;i < N;i++)
{
all.insert(str[i]);
}
int n = all.size();
int s = 0,t = 0,num = 0;
map<char,int>count;
int res = N;
for (;;)
{
while (t < N && num < n)
{
if (count[str[t++]]++ == 0)
{
num++;
}
}
if (num < n)
break;
res = min(res,t-s);
if (--count[str[s++]] == 0)
{
num--;
}
}
printf("%d\n",res);
return 0;
}
CF 701C They Are Everywhere(尺取法)的更多相关文章
- CodeForces 701C They Are Everywhere 尺取法
简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...
- codeforces 701C C. They Are Everywhere(尺取法)
题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- HDU5806:NanoApe Loves Sequence Ⅱ(尺取法)
题目链接:HDU5806 题意:找出有多少个区间中第k大数不小于m. 分析:用尺取法可以搞定,CF以前有一道类似的题目. #include<cstdio> using namespace ...
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
随机推荐
- lecture15-自动编码器、语义哈希、图像检索
Hinton第15课,本节有课外读物<Semantic Hashing>和<Using Very Deep Autoencoders for Content-Based Image ...
- 细数Javascript技术栈中的四种依赖注入
作为面向对象编程中实现控制反转(Inversion of Control,下文称IoC)最常见的技术手段之一,依赖注入(Dependency Injection,下文称DI)可谓在OOP编程中大行其道 ...
- ModernUI教程:MEF应用向导
本文主要说明在Modern UI框架下使用MEF的必要步骤,关于MEF请自行脑补. MEF-INTO-MUI实例代码下载: MefMuiApp.zip 1:创建一个导出属性 ModernFrame用来 ...
- ASP.NET Web API 安全验证之摘要(Digest)认证
在基本认证的方式中,主要的安全问题来自于用户信息的明文传输,而在摘要认证中,主要通过一些手段避免了此问题,大大增加了安全性. 1.客户端匿名的方式请求 (无认证) HTTP/ Unauthorized ...
- [HDOJ5451]Best Solver(乱搞)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5451 分析:A=5+2根号6 B=6-2根号6 n=1+2^x 那么A^n+B^n是整数 注意到0< ...
- REST API (from IBM)
REST 本身是设计风格而不是标准.REST 谈论一件非常重要的事,如何正确地使用 Web标准,例如,HTTP 和 URI.想要了解 REST 最好的方式就是思索与了解 Web 及其工作方式.如果你设 ...
- Form表单提交的简要方式
<html> <head> <meta name="viewport" content="width=device-width" ...
- clean之后R文件消失
首先确定你的SDK是新的. 其次接下来检查你的.xml文件,文件名不能大写. 如果xml文件太多 ,那么clean一下你的项目,这时候注意看Console的提示. Console会提示你xml文件错误 ...
- 开发错误记录5:Failed to resolve: com
今在导入项目时报:Failed to resolve: com.android.support:appcompat-v7:23.1.1包! 一.按F12查看包引用情况 v7包版本不一样,环境中只有co ...
- iPad开发--UIPopoverController简单使用iOS7之前和iOS7之后的使用方法
一.iOS7之前的Popover的使用 对Popover进行懒加载处理 内容控制器中设置Popover弹出后的尺寸 设置显示的位置,两种情况.1 -- 给BarButtonItem设置Popover的 ...