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

inputCopy

5 1

AABBB

outputCopy

NO

inputCopy

5 1

ABABB

outputCopy

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.

【题意】:输入一堆大写字母,每个字符从第一次出现到最后一次出现的这段时间内需要一个守卫,问你在给定k给守卫的条件下,总需求会不会超过k个守卫。

【分析】:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
typedef long long ll;
ll p[maxn];
ll x[maxn];
ll n,q;
ll a[maxn]; 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;
}
/*
输入一堆大写字母,每个字符从第一次出现到最后一次出现的这段时间内需要一个守卫,问你在给定k给守卫的条件下,总需求会不会超过k个守卫。
思路:比如说,A出现了多次,那么在A的第一次出现的位置i处num[i]++,最后一个A在j处num[j+1]--,其他字母类似,这样处理后,对num数组前n项和处理,每个位置就是那个时刻开门的个数了,然后一次遍历就行了。
因为已知访客的入口顺序, 先记录每个入口的访客数量
然后循环, 若该入口第一个访客进入, 守卫减1, 当入口最后一个访客进入, 守卫加1
其中注意守卫为负数时, 表示不能防止有人混入
抽象一下就是,求客人最多被多少个区间覆盖。
5 1
AABBB
01
24
NO 5 1
ABABB YES
*/

CF 834B The Festive Evening【差分+字符串处理】的更多相关文章

  1. [CF 295A]Grag and Array[差分数列]

    题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...

  2. [ 9.29 ]CF每日一题系列—— 765B字符串规律

    Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...

  3. 【Codeforces Round #426 (Div. 2) B】The Festive Evening

    [Link]:http://codeforces.com/contest/834/problem/B [Description] [Solution] 模拟水题; 注意一个字母单个出现的时候,结束和开 ...

  4. Codeforces Round #426 (Div. 2) B题【差分数组搞一搞】

    B. The Festive Evening It's the end of July – the time when a festive evening is held at Jelly Castl ...

  5. Lyndon 相关的炫酷字符串科技

    浅谈从 Lyndon Words 到 Three Squares Lemma By zghtyarecrenj 本文包括:Lyndon Words & Significant Suffixes ...

  6. Codeforces Round #426 (Div. 2)A B C题+赛后小结

    最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...

  7. Codeforces Round #426 (Div. 2)A题&&B题&&C题

    A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...

  8. javascript运算符与表达式

    表达式 表达式是关键字.运算符.变量以及文字的组合,用来生成字符串.数字或对象.一个表达式可以完成计算.处理字符.调用函数.或者验证数据等操作. 表达式的值是表达式运算的结果,常量表达式的值就是常量本 ...

  9. http://codeforces.com/contest/834

    A. The Useless Toy time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. BJOI2018

    BJOI2018 省选挂完,是时候更一篇题解了.对于鬼畜结论题我只放结论不给证明,不要打我-- day1 二进制 试题描述 pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不 ...

  2. ionic3自定义图标

    http://blog.csdn.net/qq993284758/article/details/78107412

  3. 如何把SSL公钥和私钥转化为PFX格式

    1.登陆   https://myssl.com/cert_convert.html 2.原格式选择为 “PEM”,目标格式选择为 “PKCS12” 3.上传cer到 ”证书文件“,上传key到 ”私 ...

  4. Grep basic and practice

    定义:Grep (Globally search for the reqular expression and print out the line). 好处:Grep 在执行时不需要先调用编辑程序, ...

  5. HTML页面为什么设置了UTF-8仍然中文乱码

    如题,其实问题很简单,在用EditPlus写html页面的时候,发现设置为UTF-8的时候仍然出现了乱码,这是一个很奇怪的问题,而且我完全考虑了浏览器的解析问题,将title放在了了meta标签之后, ...

  6. RPC-Thrift(三)

    TProtocol TProtocol定义了消息怎么进行序列化和反序列化的. TProtocol的类结构图如下: TBinaryProtocol:二进制编码格式: TCompactProtocol:高 ...

  7. C++ 异常~一 转

    C++ 异常机制分析   阅读目录 C++异常机制概述 throw 关键字 异常对象 catch 关键字 栈展开.RAII 异常机制与构造函数 异常机制与析构函数 noexcept修饰符与noexce ...

  8. UVALIVE 4330 Timer

    Description   Recently, some archaeologists discovered an ancient relic on a small island in the Pac ...

  9. phpstudy2014没有mysqldumpslow.pl及其在性能优化的作用

    mysqldumpslow.pl的作用是监控mysql的性能瓶颈的 1)在phpstudy2014中没有这个mysqldumpslow.pl所以需要去下载一个然后放置于mysql/bin的文件夹中,由 ...

  10. golang命令行参数解析

    package main import ( "fmt" "os" ) func main(){ s:= os.Args fmt.Println(s) } 直接执 ...