2018.12.15【NOIP提高组】模拟B组 JZOJ 100046 收集卡片

版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/85014615

题目

JZOJ 100046 收集卡片


思路

维护一个区间,枚举结束订阅的时间判断能否晚点订阅,开一个变量模拟指针维护即可。
——鸣谢 w y c wyc 大佬考场救急

时间复杂度: O ( n ) O(n)


代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;int n,tong[53],ans=0x3f3f3f3f,num,i=1,now=0;
char s[500001];
signed main()
{
	scanf("%d\n",&n);
	scanf("%s",s+1);
	for(register int i=1;i<=n;i++) 
	{
		if(s[i]>96) s[i]-=5;
		tong[s[i]-65]=true;//因为大小写
	}
	for(register int i=0;i<53;i++) if(tong[i]) num++;
	memset(tong,0,sizeof(tong));
	for(register int j=1;j<=n;j++)
	{
		tong[s[j]-65]++;
		while(tong[s[i]-65]>1)//判断头指针能否往前移
		{
			tong[s[i]-65]--;
			i++;
		}
		now=0;
		for(register int k=0;k<53;k++) if(tong[k]) now++;//判断是否合法
		if(now==num) ans=min(ans,j-i+1);
	}
	printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/85014615
今日推荐