题目链接: 传送门

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(尺取法)的更多相关文章

  1. CodeForces 701C They Are Everywhere 尺取法

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

  2. codeforces 701C C. They Are Everywhere(尺取法)

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

  3. HDU5806:NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:HDU5806 题意:找出有多少个区间中第k大数不小于m. 分析:用尺取法可以搞定,CF以前有一道类似的题目. #include<cstdio> using namespace ...

  4. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  5. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  6. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  7. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

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

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

  9. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

随机推荐

  1. 拥抱 HTML5:storage 简介以及使用方法

    前言 storage 其实是个很简单的东西,基本上只要知道 javascript 中对象的概念,然后读完此文,storage 的用法也就了然于胸了. 简单来说,你可以把 storage 想象成是储存在 ...

  2. Scala函数式编程进阶

    package com.dtspark.scala.basics /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式 ...

  3. 关于Node.js的httpClieint请求报错ECONNRESET的原因和解决措施

    背景说明 最近在工作项目中有下面一个场景: 使用Node.js的express框架实现了一个文件系统服务器端,其中有个API用于客户端上传文件.客户端使用Node.js的HttpClient来调用服务 ...

  4. 中晟银泰国际中心酒店式公寓介绍 业主交流QQ群:319843248

    行政区域:中原区 区域板块:西北板块 项目位置:中原路与华山路东北角(中原万达北侧中原西路对面) 建筑类型:高层 物业类别:酒店式公寓 户型面积:公寓35-100平米 开发商:中晟集团 投资商:中晟集 ...

  5. SQLite剖析之C/C++接口

    前言 SQLite3是SQLite一个全新的版本,它虽然是在SQLite2的代码基础之上开发的,但是使用了和之前的版本不兼容的数据库格式和API.SQLite3是为了满足以下的需求而开发的:支持UTF ...

  6. 根本没有“JSON“对象这回事(读汤姆大叔博文记录)

    1.字面量 (1)他们是固定的值,不是变量,让你从“字面上”理解脚本. (2)字符串字面量是由双引号("")或单引号('')包围起来的零个或多个字符串组成的. (3)对象字面量是由 ...

  7. 如何限制虚拟主机可使用的CPU资源

    使用IIS 6.0运营虚拟主机的朋友们都会碰到这样一个问题,当某个网站占用大量CPU资源时,会把整个服务器都拖慢了,影响服务器上其他网站的访问速度,客户们的投诉也让系统管理员倍感头疼.我们知道,从II ...

  8. GBDT(MART) 迭代决策树简介

    以下对GBDT的介绍深入浅出,非常易懂 转自:http://blog.csdn.net/w28971023/article/details/8240756 GBDT(Gradient Boosting ...

  9. CSS与JQuery的相关问题

    文字隐藏:p div里面的文字过长时隐藏文字: overflow:hidden; text-overflow:ellipsis; white-space:nowrap; --------------- ...

  10. 【JavaScript】 JSON

    1.json 一共就这么几种数据类型   ①,number   和JavaScript的number完全一样   ②,boolean  就是JavaScript的true和false   ③,stri ...