码迷,mamicode.com
首页 > 其他好文 > 详细

UOJ #578. 收集卡片

时间:2019-09-24 13:50:52      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:main   oid   区分大小写   class   描述   char s   pac   star   algo   

【题目描述】:
Star计划订购一本将要发行的周刊杂志,但他可不是为了读书而是集卡。

已知杂志将要发行N周(也就是N期),每期都会附赠一张卡片。Star通过种种途径,了解到N期杂志附赠的卡片种类。Star只想订购连续的若干期, 并在这些期内收集到所有可能出现的种类的卡片。现在他想知道,最少需要订 购多少期。

【输入描述】:
第一行一个整数 N;

第二行一个长度为 N 的字符串,由大写或小写字母组成,第 i 个字符表示第i 期附赠的卡片种类,每种字符(区分大小写)表示一种卡片。

【输出描述】:
输出一行一个整数,表示 Star 最少订购的期数。

【样例输入】:
8
acbbbcca
【样例输出】:
3
【样例说明】:
【时间限制、数据范围及描述】:
时间:1s 空间:256M

对于 30%的数据,N≤300;

对于 40%的数据,N≤2000;

对于 60%的数据,N≤5000;

对于 80%的数据,N≤100000;

对于100%的数据,N≤500000。

本题直接尺取大法搞定.

Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=500010;
char str[MAXN];
bool mark[128];
int n,m,cnt[128];
inline void init()
{
    scanf("%d%s",&n,str);
    for(int i=0;i<n;i++)
        if(!mark[str[i]])m++,mark[str[i]]=true;
}
inline int solve()
{
    int k=0,st=0,ed=0,ret=n;
    while(st<n)
    {
        while(ed<n&&k<m)
            if(!(cnt[str[ed++]]++))k++;
        if(k==m)ret=min(ret,ed-st);
        if(!(--cnt[str[st++]]))k--;
    }
    return ret;
}
int main()
{
    init();
    printf("%d\n",solve());
    return 0;
}

UOJ #578. 收集卡片

标签:main   oid   区分大小写   class   描述   char s   pac   star   algo   

原文地址:https://www.cnblogs.com/ukcxrtjr/p/11577807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!