16CF1-B

来源:互联网 发布:多益网络运维面试经验 编辑:程序博客网 时间:2024/04/29 10:51

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


1、题意:城堡会来n个客人,一共有k个保卫城堡的人(看门的),每个客人用大写字母表示他从哪个门进入城堡,在每个门的客人没有进完成之前,这个门口的保安不能离开,按照顺序每次进入一个客人,当同时看守门的保安数目大于保安的总数时输出YES,否则输出NO。
2、思路:用string保存客人,然后再用一个数组表示每个门客人的数目,另一个数组表示这里是否有保安,每当该客人进入的门没有保安的时候,当前正在看守门的保安数目+1,当从该门进入的客人为该门的最后一个时,当前看守门的保安数目-1,设置个最大值,每次比较即可,最后最大值与保安数比较即可知结果。
3、代码:

#include<iostream>#include<string>#include<cstring>using namespace std;int main(){string p;int m,n,s[26],x,i,max;int b[26];while(cin>>m>>n>>p){memset(s,0,sizeof(s));memset(b,0,sizeof(b));for(i=0;i<m;i++)s[int(p[i]-'A')]++;max=0;x=0;for(i=0;i<m;i++){if(b[int(p[i]-'A')]==0){x++;if(x>max)max=x;b[int(p[i]-'A')]=1;}s[int(p[i]-'A')]--;if(s[int(p[i]-'A')]==0)x--;}if(max>n)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;}


4、总结:读懂题这题不难,一遍过。

原创粉丝点击