Codeforces Round #426 (Div. 2) B. The Festive Evening(思维)

来源:互联网 发布:裴乐品牌知乎 编辑:程序博客网 时间:2024/06/03 21:20
B. 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 fromA toZ. 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 arek such guards in the castle, so if there are more thank 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 thank 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 letterss1s2...sn are given, wheresi is the entrance used by thei-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.

思路:比如说,A出现了多次,那么在A的第一次出现的位置 i 处 num[i]++  ,最后一个 A j 处 num[j+1] -- ;其他字母类似,这样处理后,对num数组前n项和处理,每个位置就是那个时刻开门的个数了,然后一次遍历就行了。

#nclude <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <algorithm>#include <iostream>using namespace std;char s[1000100];map <char,int>mp;int num[1000100];int main(){    int n,k;    scanf("%d%d",&n,&k);    getchar();    gets(s);    for(int i = 0; s[i]; i++)    {        if(mp[s[i]] == 0)        {            num[i] ++;        }        mp[s[i]] ++;    }    for(int i = 0; s[i]; i++)    {        mp[s[i]] --;        if(mp[s[i]] == 0)        {           num[i+1] --;        }    }    for(int i = 1; s[i]; i++)    {        num[i] += num[i-1];    }    for(int i = 0; s[i]; i++)    {        if(num[i] > k)        {            printf("YES\n");            return 0;        }    }    printf("NO\n");    return 0;}

原创粉丝点击