The Festive Evening map

来源:互联网 发布:淘宝买家要求延迟发货 编辑:程序博客网 时间:2024/05/23 02:00
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 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 1
AABBB
output
NO
input
5 1
ABABB
output
YES
题意:
一共26个门,k个守卫,n个客人,每个客人只能从指定的一扇门进入。守卫在第一个客人到达之前站在指定门口,最后一个从该门进去的客人来到后,守卫才能离开前往别的门。求最少需要多少守卫才能使所有客人都能见到守卫。
思路:两个map,一个记录状态,一个记录人数。
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<map>using namespace std;int main(){    int n,k;    map<char,int> m;    map<char,int> v;    char str[1000005];    while(~scanf("%d%d",&n,&k))    {        int cnt=1,tag=0;        m.clear();v.clear();        memset(str,0,sizeof(str));        scanf("%s",str);        for(int i=0;i<n;i++)             m[str[i]]++;        m[str[0]]--;v[str[0]]=1;         for(int i=1;i<n;i++)         {             if(!m[str[i-1]]) tag++;             if(v[str[i]]==0)             {                 v[str[i]]=1;                 cnt++;                 if(tag) {cnt--;tag--;}             }             m[str[i]]--;         }         if(cnt>k) printf("YES\n");         else printf("NO\n");    }    return 0;}


原创粉丝点击