CF 834B-The Festive Evening

来源:互联网 发布:阿里云的意义 编辑:程序博客网 时间:2024/06/07 10:17

The Festive Evening
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Examples
input
5 1AABBB
output
NO
input
5 1ABABB
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.


题意:有26个门,分别是A到Z,现在有n个人想进门,有k个守卫,每个守卫只能守一个门,直到自己守得门关

闭为止,才能移动到别的门去守,每个入口的门在第一个人到达之前被打开,并在最后一个人到达之后

关闭,不能有两个人同时进一个门。如果在某一时刻至少有一扇门没有被守,则输出YES,否则输出NO

分析:本题wa了5次,从一开始简单的思路开始wa,然后改正想法,将输入的数组从前向后遍历,用数组标记字

母第一次出现的位置(数组下标用字母表示,存的值是字母出现的位置),再用一个数组从后往前遍历,标

记第一个字母出现的位置(也是字母的最后出现位置),但是又wa了,比如这组测试数据:

10 1
ZXCVBNMASZ

本应输出YES,但按照上述思路则不行。然后还要注意一组数据,如下(本人感觉这题好坑呀):

10 1
ZXCVBNMASD

不明白的小伙伴结合题意和自己的理解看看如下代码:

#include<stdio.h>#include<string>#include<string.h>#include<iostream>#include<algorithm>using namespace std;int a[1000005];//每个字母对应的位置int b[100];//代表字母第几次出现int main(){    memset(b,0,sizeof(b));    memset(a,0,sizeof(a));    int n,k;    string s;    scanf("%d%d",&n,&k);    cin>>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;    int maxx=0;    for(int i=0; i<n; i++)    {        sum+=a[i];        maxx=max(maxx,sum);    }    if(maxx>k)        printf("YES\n");    else        printf("NO\n");    return 0;}




原创粉丝点击