CodeForces 834B的解题思路是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计780个文字,预计阅读时间需要4分钟。
七月末,Jelly Castle举办盛大的节日晚会。来自各地的宾客汇聚一堂,探讨糖果世界的最新潮流。然而,这里讨论的一些内容并不应该公开。
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 fromAtoZ. 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 areksuch guards in the castle, so if there are more thank
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
Input
Two integers are given in the first string: the number of guestsnand the number of guardsk(1 ≤ n ≤ 106,1 ≤ k ≤ 26).
In the second string,nuppercase English letterss1s2...snare given, wheresiis 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).
Example
Input
5 1
AABBB
Output
NO
Input
5 1
ABABB
Output
YES
题目大概:
有客人要进城堡,城堡有26个门,每个门都可以进。当有人要从一个门进的时候必须要有守卫看守,当这个门的最后一个人进入后,守卫才可以离开,中间不能离开,问是否要添加守卫。
思路:
把这些门都设为数组,有人要从该门进,就a[]++,并要有守卫。当进去后再把这个人减出来,当人为零是该门就不需要守卫了。看看是否守卫数量是负数。
代码:
#include <iostream>#include <string>
#include <queue>
#include <cstring>
using namespace std;
int b;
int a[27]={0},p[27]={0};
int main()
{int n,f=0;
cin>>n>>b;
string s;
cin>>s;
for(int i=0;i<n;i++)
{
a[int(s[i]-64)]++;
}
for(int i=0;i<n;i++)
{
a[int(s[i]-64)]--;
if(p[int(s[i]-64)]==0){b--;p[int(s[i]-64)]=1;}
if(b<0){f=1;break;}
if(a[int(s[i]-64)]==0)b++;
}
if(f)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
本文共计780个文字,预计阅读时间需要4分钟。
七月末,Jelly Castle举办盛大的节日晚会。来自各地的宾客汇聚一堂,探讨糖果世界的最新潮流。然而,这里讨论的一些内容并不应该公开。
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 fromAtoZ. 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 areksuch guards in the castle, so if there are more thank
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
Input
Two integers are given in the first string: the number of guestsnand the number of guardsk(1 ≤ n ≤ 106,1 ≤ k ≤ 26).
In the second string,nuppercase English letterss1s2...snare given, wheresiis 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).
Example
Input
5 1
AABBB
Output
NO
Input
5 1
ABABB
Output
YES
题目大概:
有客人要进城堡,城堡有26个门,每个门都可以进。当有人要从一个门进的时候必须要有守卫看守,当这个门的最后一个人进入后,守卫才可以离开,中间不能离开,问是否要添加守卫。
思路:
把这些门都设为数组,有人要从该门进,就a[]++,并要有守卫。当进去后再把这个人减出来,当人为零是该门就不需要守卫了。看看是否守卫数量是负数。
代码:
#include <iostream>#include <string>
#include <queue>
#include <cstring>
using namespace std;
int b;
int a[27]={0},p[27]={0};
int main()
{int n,f=0;
cin>>n>>b;
string s;
cin>>s;
for(int i=0;i<n;i++)
{
a[int(s[i]-64)]++;
}
for(int i=0;i<n;i++)
{
a[int(s[i]-64)]--;
if(p[int(s[i]-64)]==0){b--;p[int(s[i]-64)]=1;}
if(b<0){f=1;break;}
if(a[int(s[i]-64)]==0)b++;
}
if(f)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}

