zoukankan      html  css  js  c++  java
  • 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
    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
    Note

    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.

    思路: 尺取法模版题

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int N=1e5+10,M=1e6+10,mod=1e9+7,inf=1e9+10;
    int flag[100];
    int getnum(char a)
    {
        if(a>='A'&&a<='Z')
        return a-'A';
        return a-'a'+30;
    }
    char a[N];
    int main()
    {
        int x,y,z,i,t;
        int cnt=0;
        scanf("%d%s",&x,&a);
        for(i=0;i<x;i++)
        {
            if(flag[getnum(a[i])]==0)
            cnt++;
            flag[getnum(a[i])]++;
        }
        memset(flag,0,sizeof(flag));
        int st=0,en=0,gg=0;
        int ans=inf;
        while(1)
        {
            while(en<x&&gg<cnt)
            {
                if(flag[getnum(a[en])]==0)
                gg++;
                flag[getnum(a[en])]++;
                en++;
            }
            if(gg<cnt)break;
            ans=min(en-st,ans);
            flag[getnum(a[st])]--;
            if(flag[getnum(a[st])]==0)
            gg--;
            st++;
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    使用子查询可提升 COUNT DISTINCT 速度 50 倍
    页面装载js及性能分析方法
    用CSS创建打印页面
    每个Web开发者都应该知道的关于URL编码的知识
    C IO programming test code
    全球NTP服务器列表
    MySQL数据的查询注意
    Python使用pyMysql模块插入数据到mysql的乱码解决
    单元测试
    python threading.thread
  • 原文地址:https://www.cnblogs.com/jhz033/p/5700595.html
Copyright © 2011-2022 走看看