题目链接:

C. They Are Everywhere

time limit per test

2 seconds

 
memory limit per test

256 megabytes

input

standard input

output

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.

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.

Examples
input
3
AaA
output
2
input
7
bcAAcbc
output
3
input
6
aaBCCe
output
5

题意:

问连续子序列包含所有类型字母的最短长度是多少;

思路:

尺取法,枚举左端点,移动右端点;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=500+10;
const double eps=1e-6; int n,num[60],vis[60],a[N];
char s[N]; int check(char x)
{
if(x>='a'&&x<='z')return x-'a';
else return x-'A'+26;
}
int main()
{
read(n);
scanf("%s",s+1);
int cnt=0;
For(i,1,n)
{
a[i]=check(s[i]);
//if(!vis[a[i]])cnt++;
vis[a[i]]=1;
}
For(i,0,59)if(vis[i])cnt++;
//cout<<cnt<<endl;
int ans=inf;
int l,r=1,sum=0;
For(i,1,n)
{
l=i;
while(1)
{
if(r>n)break;if(sum>=cnt)break;
num[a[r]]++;
if(num[a[r]]==1)sum++; r++;
}
//cout<<r<<" "<<sum<<endl;
if(sum>=cnt)ans=min(ans,r-l);
num[a[i]]--;
//cout<<num[a[i]]<<endl;
if(num[a[i]]==0)sum--;
}
cout<<ans<<"\n";
return 0;
}

  

codeforces 701C C. They Are Everywhere(尺取法)的更多相关文章

  1. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  2. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

  3. [CF660C]Hard Process(尺取法)

    题目链接:http://codeforces.com/problemset/problem/660/C 尺取法,每次遇到0的时候补一个1,直到补完或者越界为止.之后每次从左向右回收一个0点.记录路径用 ...

  4. CodeForces 701C They Are Everywhere 尺取法

    简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...

  5. Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法

    D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...

  6. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  7. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  8. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

  9. Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)

    题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. cookie读取设置name

    cookie就是k-v形式,可以理解为一个hashmap cookie就是k-v形式,可以理解为一个hashmap cookie就是k-v形式,可以理解为一个hashmap 建立一个无生命周期的coo ...

  2. jmeter如何在写入jtl文件时同步写入数据库

    参考:1.http://blog.csdn.net/cakushin7433/article/details/53367508    2.http://blog.csdn.net/cakushin74 ...

  3. Android基础新手教程——3.7 AnsyncTask异步任务

    Android基础新手教程--3.7 AnsyncTask异步任务 标签(空格分隔): Android基础新手教程 本节引言: 本节给大家带来的是Android给我们提供的一个轻量级的用于处理异步任务 ...

  4. C 标准库 - <limits.h>

    C 标准库 - <limits.h> 简介 limits.h 头文件决定了各种变量类型的各种属性.定义在该头文件中的宏限制了各种变量类型(比如 char.int 和 long)的值. 这些 ...

  5. android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

    为了使图片浏览器左右无限循环滑动 我们要自己定义gallery的adapter 假设要想自己定义adapter首先要了解这几个方法 @Override public int getCount() { ...

  6. C++中sizeof(struct)怎么计算?(转)

    struct为空时,大小为1. 1. sizeof应用在结构上的情况 请看下面的结构: struct MyStruct { double dda1; char dda; int type; }; 对结 ...

  7. 从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能

    这一章节我们通过讨论装载因子与性能,再来介绍HashMap的工作原理. 1.什么是装载因子?他有什么作用? 以下的代码就是装载因子 /** * The load factor used when no ...

  8. 设置Ubuntu 16.04 LTS的Unity启动器的位置命令

    将Ubuntu 16.04 LTS的Unity启动器移动到桌面底部命令:gsettings set com.canonical.Unity.Launcher launcher-position Bot ...

  9. Java Base64加密、解密原理Java代码(转载)

    博客来源:http://blog.csdn.net/songylwq/article/details/7578905 Base64是什么: Base64是网络上最常见的用于传输8Bit字节代码的编码方 ...

  10. 解读OC中的load和initialize

    在 Objective-C 中,NSObject 是绝大多数类的基类.而在 NSObject 中有两个类方法 load 和 initialize,那这两个方法是在什么时机被调用呢?父类.Categor ...