jzoj100046. 收集卡片

唉,本来都不想写这一题的。。。

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

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

Output
输出一行一个整数,表示 Star 最少订购的期数。

Sample Input
8 acbbbcca

Sample Output
3

这题两个指针不停地跳即可。
上标:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[500010],l=1,r=1,now[500010],ans;
bool bz[500010];char c;

bool check()
{
	for (int i=1;i<=52;i++)
		if (bz[i]==1 && !now[i]) return 0;
	return 1;
}

int main()
{
	scanf("%d\n",&n);
	for (int i=1;i<=n;i++)
		if ((c=getchar())>='a') a[i]=c-'a'+1,bz[a[i]]=1;
		else a[i]=c-'A'+27,bz[a[i]]=1;
	now[a[1]]++;while (!check()) now[a[++r]]++;ans=r;
	while (l<=n)
	{
		now[a[l++]]--;
		while (now[a[l]]>1) now[a[l++]]--;
		while (!check() && r<=n) now[a[++r]]++;
		if (r>n) break;
		ans=min(ans,r-l+1);
	}
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Larry1118/article/details/85014906