The Festive Evening
来源:互联网 发布:mac word繁体变简体 编辑:程序博客网 时间:2024/05/22 11:55
B. The Festive Evening time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard 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 ≤ 106, 1 ≤ 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
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.
当时把输入给注释了死活没找出错来,后来才发现,也是一个思维题,统计每个字母的个数,用的ascii码做的处理每一次都++,然后再循环里处理,当门是空的时候把一个保安给空出来b[i]相当于判重数组,操作一次把b[i]变为0
#include<iostream>#include<cstring>#include<string>using namespace std;char s[10000010];int main(){ int n,k,cnt; int a[27],b[27]; //while(cin>>n>>k) //{ cin>>n>>k; cnt=0; for(int i=0;i<n;i++) cin>>s[i]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=0;i<n;i++) a[s[i]-'A']++; int i; for(i=0;i<n;i++) { if(!b[s[i]-'A']) { cnt++; b[s[i]-'A']=1; } if(cnt>k) { cout<<"YES"<<endl; break; } a[s[i]-'A']--; if(!a[s[i]-'A']) cnt--; } if(i==n) cout<<"NO"<<endl; //} return 0;}
- Codeforces834B The Festive Evening
- CF834B-The Festive Evening
- The Festive Evening map
- The Festive Evening
- The Festive Evening
- The Festive Evening (Codeforces
- cf The Festive Evening(水)
- The Festive Evening(思维题)
- 守卫问题The Festive Evening
- CodeFroces 834B. The Festive Evening(暴力)
- Codeforces 834B-The Festive Evening
- 【CodeForces】426Div2 B The Festive Evening
- Codeforces Round 834B-The Festive Evening
- Codeforces 834 B The Festive Evening
- CodeForces #426 div2 B The Festive Evening
- CodeForces 834B The Festive Evening
- The Festive Evening (思维题目)
- CF 834B-The Festive Evening
- 20170801 建军大业
- python+eclipse+pydev安装配置
- 如何解决跨域问题?
- Ubuntu 14.04 LTS下安装Google Chrome浏览器
- 细说JDK动态代理的实现原理
- The Festive Evening
- 常见问题
- 常见的块状元素(block)与内联元素(inline)
- ORACLE数据库数据操作语言DML
- 第一次发博客,不知写点啥,就来个hello world吧!
- 【BZOJ】1030 [JSOI2007]文本生成器 AC自动机+DP
- 设计模式之代理模式
- 【回文串】835D Palindromic characteristics
- python 爬取网页的最基础三种方法