16CF1--B-1002

It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in.

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 kopened 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 ≤ 1061 ≤ 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).

Example
Input
5 1
AABBB
Output
NO
Input
5 1
ABABB
Output
YES
Note

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.


题意:

n个客人每秒一个,k个守卫,每个客人走各自对应的门,走同一个门的客人用相同符号表示,给出符号序列,一个门在第一个客人来时开启,在最后一个客人进入后关闭,判断是否有某个时刻,有门没有守卫。

解题思路:

这个题可能会想到很多方法,但是由于1<=n<=1e6,所以要找一个不会超时的方法。用两个数组分别记录每个门开启和关闭的时间,然后对于时间循环,每个时间判断有几个门是开启的

代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
using namespace std;
int main()
{
    int n,k,i,j,num;
    char c;
    int s[27]={0};
    int e[27]={0};
    scanf("%d%d",&n,&k);
    getchar();     //第一次提交,没加这一句,所以多错了一次
    for(i=1;i<=n;i++)
    {
        scanf("%c",&c);
        //cout<<i<<" "<<(c-'A')<<endl;
        if(s[c-'A']==0) s[c-'A']=i;
        e[c-'A']=i;
    }
    bool flag=true;
    for(i=1;i<=n;i++)  //循环时间
    {
        num=0;
        for(j=0;j<=25;j++)  //循环门
        {
            if(s[j]<=i&&e[j]>=i) num++; //门在开启
        }
        if(num>k) {flag=false; break;} //判断好就退出节省时间
    }
    if(flag==false) printf("YES\n");
    else printf("NO\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值