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.

    题意:输入一串字符串,统计字符种类,然后求最小 ”包括所有种类字符“ 的子串的长度。

    思路:   1.可以用set来统计字符种类type

        2.两层for循环。第一层从0到n;第二层只遍历0到n,途中用vv[]记录各个字符出现的次数,字符第一次出现则len++,当len==type时跳出第二层循环,求res=min(res,j-i),同时将第一层循环下标为i的字符的vv值-1,如果vv值变为0,则len--。复杂度O(n)

    代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <set>
    #define ll long long
    using namespace std;
    int main()
    {
        int n,i,j;
        char s[100100];
        int vv[150];
        scanf("%d",&n);
        scanf("%s",&s);
        int type;
        set<char>st;
        for(i=0;i<n;i++)
            st.insert(s[i]);
        type=st.size();
        memset(vv,0,sizeof(vv));
        j=0;
        int len=0;
        int res=n;
        for(i=0;i<n;i++)
        {
            for(j;j<n;j++)
            {
                if(len==type)break;
                if(!vv[s[j]])len++;
                vv[s[j]]++;
            }
            if(len==type) res=min(res,j-i);
            vv[s[i]]--;
            if(!vv[s[i]]) len--;
        }
        printf("%d ",res);
    }
  • 相关阅读:
    记一次Postgres CPU爆满故障
    Bug 28450914 : ORA-600: [KDLRCI_GET_INLINE_DATA] SELECTING FROM CDB_FEATURE_USAGE_STATISTICS
    获取数据表中列的描述值
    【自考】数据结构第四章树和二叉树,期末不挂科指南,第6篇
    SqlServer视图查询效率测试
    MySQL中使用group by 是总是出现1055的错误
    redis中key键操作
    SQL Server等待事件&mdash;PAGEIOLATCH_EX
    MySQL 什么是索引?
    计算经历的时间
  • 原文地址:https://www.cnblogs.com/bestwzh/p/6075553.html
Copyright © 2011-2022 走看看