The doorman Bruno at the popular night club Heaven is having
a hard time fulfilling his duties. He was told by the owner
that when the club is full, the number of women and men let into
the club should be roughly the same. When the night club opens,
people wanting to enter the club are already lined up in a queue,
and Bruno can only let them in one-by-one. He lets them in moreor-less
in the order they are lined up. He can however decide to let
the second person in the queue cut the line and slip into the club
before the person in front. This will no doubt upset the person first
in line, especially when this happens multiple times, but Bruno is
quite a big guy and is capable of handling troublemakers. Unfortunately
though, he is not that strong on mental calculations under
these circumstances. He finds keeping track of the difference of the number of women and number of men let into
the club a challenging task. As soon as the absolute difference gets too big, he looses track of his counting and
must declare to the party people remaining in the queue that the club is full.
Input
The first line of input contains a positive integer X < 100 describing the largest absolute difference between the
number of women and number of men let into the club, that Bruno can handle. The second line contains a string
consisting solely of the characters ’W’ and ’M’ of length at most 100, describing the genders of the people in the
queue, in order. The leftmost character of the string is the gender of the person first in line.
Output
The maximum number of people Bruno can let into the club without loosing track of his counting. You may
assume that the club is large enough to hold all the people in the queue.
Sample Input 1 Sample Output 1
1
MWWMWMMWM
9
Sample Input 2 Sample Output 2
2
WMMMMWWMMMWWMW
8
NCPC 2012 Problem D: Doorman 7
This pag
题意:
一个门卫统计男女数差值最大不超过k,当超过k时,他能看到下一位的性别如果差值不能大于k就接着放人进
解:
直接模拟
#include<bits/stdc++.h>
using namespace std;
int main()
{
int k,i,j;
string s;
while(cin>>k>>s)
{
map<char ,int> q;
q.clear();
int len=s.length();
for(i=0;i<len;i++)
{
q[s[i]]++;
if(abs(q['W']-q['M'])>k&&i+1<len)
{
if(s[i]!=s[i+1])
{
q[s[i+1]]++;
i++;
}
else
{
goto loop;
}
}
}
loop:
cout<<i<<endl;
}
return 0;
}