题目描述
There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.
For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are k such guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed.
Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.
输入
In the second string, n uppercase English letters s1,s2... sn are given, where si is the entrance used by the i-th guest.
输出
You can output each letter in arbitrary case (upper or lower).
样例输入
5 1
AABBB
样例输出
NO
提示
In the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed
after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.
In the second sample case, the door B is opened before the second guest's arrival, but the only guard can't leave the door A unattended, as there is still one more guest that should enter the castle through this door.
来源
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int b[30],a[100005];
char s[100005];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
getchar();
gets(s);
for(int i=0;i<n;i++)
{
if(b[s[i]-'A']==0)
a[i]++;
b[s[i]-'A']++;
}
for(int i=0;i<n;i++)
{
b[s[i]-'A']--;
if(b[s[i]-'A']==0)
a[i+1]--;
}
int sum=0,maxn=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
maxn=max(maxn,sum);
}
if(maxn>k) printf("YES\n");
else printf("NO\n");
return 0;
}