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


    题意:

    问连续子序列包含所有类型字母的最短长度是多少;

    思路:

    尺取法,枚举左端点,移动右端点;

    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('
    ');
    }
    
    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<<"
    ";
            return 0;
    }
    

      

  • 相关阅读:
    C语言|作业07
    Cookie应用
    刷新页面,怎么做到不提示“不重新发送消息,则无法刷新页面”
    笔记
    笔记
    元素内部设定position
    企业微信正式发布 Tita 绩效宝,助力企业完成数字化绩效管理转型
    OKR实践:如何获得高层的理解与关注
    OKR:衡量结果和解决实际问题
    2022 年绩效评估,HR看这一篇就够了!
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5698394.html
Copyright © 2011-2022 走看看