#426 (Div. 2) B. The Festive Evening

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.

Input
Two integers are given in the first string: the number of guests n and the number of guards k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26).

In the second string, n uppercase English letters s1s2… sn are given, where si is the entrance used by the i-th guest.

Output
Output «YES» if at least one door was unguarded during some time, and «NO» otherwise.

You can output each letter in arbitrary case (upper or lower).

Examples
input
5 1
AABBB
output
NO
input
5 1
ABABB
output
YES

题意:

给定n个guest,k个守卫,n个guest进入各自的门,当第一个guest进入的时候守卫应该去相应的门去守卫,直至最后一个guest进入。守卫不能小于0.map暴力模拟。
一个Map表示门的开关
一个Map表示该扇门客人剩余数量
wrong answer一次。原因在当第二个门开的时候,k–,应当先判断k是否小于0,再进行后续判断。

代码:

#include <bits/stdc++.h>

using namespace std;
map<char,int> num;
map<char,int> judge;
typedef long long LL;
int main()
{
    LL n;int k;
    cin>>n>>k;
    string str;
    cin>>str;
    for(int i=0;i<n;i++)
    {
        num[str[i]]++;
        judge[str[i]]=0;
    }
    bool flag=1;
    for(int i=0;i<n;i++)
    {
        if(judge[str[i]]==0) {judge[str[i]]=1;k--;}
        if(k<0) {flag=0;break;}
        if((--num[str[i]])==0) {judge[str[i]]=0;k++;}
    }
    if(flag) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值