1. 程式人生 > >【CodeForces】426Div2 B The Festive Evening

【CodeForces】426Div2 B The Festive Evening

連結:http://codeforces.com/contest/834/problem/B

Solution
暴力計算一下每個時刻有多少門開啟就好
注意不要先關門再判斷

#include<stdio.h>
#define N 1000005
int n,k,tot;
bool vis[26],st[N],ed[N];
char s[N];

int main()
{
	scanf("%d%d",&n,&k);
	scanf("%s",s+1);
	for (int i=1;i<=n;i++) if (!vis[s[i]-'A']) vis[s[i]-'A']=1,st[i]=1;
	for (int i=0;i<26;i++) vis[i]=0;
	for (int i=n;i;i--) if (!vis[s[i]-'A']) vis[s[i]-'A']=1,ed[i]=1;
	for (int i=1;i<=n;i++)
	{
		if (k<(tot+=st[i]))
		{
			puts("YES");
			return 0;
		}
		tot-=ed[i];
	}
	puts("NO");
}