题目链接:

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. 6.【nuxt起步】-完成一个静态的页面

    1.接下来新建/component/maincontent.vue 把这些html代码copy到maincontent.vue 发现格式比较难看,就格式化一下 2.插件安装 beautify,安装后重 ...

  2. Python基础语法01

    Python 标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以 ...

  3. android wifi相关模块 命令列表 汇总

    static final int BASE =Protocol.BASE_WIFI;  131072 static final intCMD_START_SUPPLICANT  = BASE +11; ...

  4. win10任务管理器开机老是自己打开

    win10任务管理器开机老是自己打开 学习了:https://zhidao.baidu.com/question/332868722086816045.html 还没有注意过这个东西:系统失败-> ...

  5. Cocos2d-x初识

    cocos2d-x引擎是什么 在学习游戏的时候就有意的搜索这方面的知识,知道游戏须要游戏引擎,引擎听着非常厉害,只是就是一个游戏框架. 或许某一个游戏框架火起来了,就非常流行了,只是我觉得不论什么游戏 ...

  6. mysql大数据量下修改表结构的方法

    http://www.blogjava.net/anchor110/articles/361152.html

  7. 我的vim插件列表

    一.正在使用的插件 1. NERD tree   文件浏览 2. bufexplorer  buffer 浏览 3. mru.vim   最近使用的文件浏览 4. ctrlp.vim  文件模糊搜索, ...

  8. Swift中的switch 和 do while

    switch后面的()能够省略 OC中的switch假设没有break就会穿透(依次运行),在Swift中不会穿透(可理解默认就有break) OC中入股要在case中定义变量,必要要加上{}确定作用 ...

  9. pandas-数据分析

    pandas是一个强大的python数据分析的工具包 pandas是基于numpy构建的 pandas的主要功能: 具备对其功能的数据结构DataFrame.Series 集成世间序列功能 提供丰富的 ...

  10. LeetCode120——Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...