zoukankan      html  css  js  c++  java
  • Codeforces 701C. 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 numbern - 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.

    题目链接:http://codeforces.com/contest/701/problem/C


    题意:输出包含所有出现过的字母的最短子串的长度。

    思路:设子串的开始为s=1,结束为当前字母的位置。统计每种字母出现的次数。如果s位置的字母出现的次数大于1将s后移直至不能移动为止。设ans为输出的答案,判断当前i-s+1与ans的大小。如果小的话ans=i-s+1。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    char let[100100];
    int cou[100];
    int main()
    {
        int i,n;
        scanf("%d",&n);
        getchar();
        memset(cou,0,sizeof(cou));
        int ans=0;
        int s=1;
        for(i=1; i<=n; i++)
        {
            scanf("%c",&let[i]);
            if(cou[let[i]-'A']==0) ans=i-s+1;
            cou[let[i]-'A']++;
            while(s<i)
            {
                if(cou[let[s]-'A']>1)
                {
                    cou[let[s]-'A']--;
                    s++;
                }
                else break;
            }
            if(ans>i-s+1) ans=i-s+1;
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    servlet的提交
    servlet的doPost 和doGet和web文件结构
    helloServlet
    捕鱼达人
    The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt
    ACM-ICPC 2017 Asia Urumqi A. Coins
    Nordic Collegiate Programming Contest 2015​ B. Bell Ringing
    变量
    hiho 1050 树的直径
    ACM-ICPC 2017 Asia Urumqi G. The Mountain
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5707388.html
Copyright © 2011-2022 走看看