Doorman

来源:互联网 发布:2017java程序设计竞赛 编辑:程序博客网 时间:2024/06/08 00:21

Description

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 more-or-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

2WMMMMWWMMMWWMW

Sample Output

8

HINT

题意:第一行 X 表示能够容忍的不能配对的人数      第二行一条 100 个字符以内的字符串'W'代表女人, 'M'代表男人      看门人一次只让一个人进去,男女尽量配对,如果不配对的人数超过了 X      那么看们人就分不清了,此时他就会宣布俱乐部人满,不再让后面的人进入      输出能进去的最多的人数。

思路:

每次进入一个人就判断一次男女不配对的人数,直到不合法。
注意:关于分不清的时候的跳出判断情况,要一直判断到男女之差 > X+1,因为即便是最多只能容许 X 个人没有配对,查找到了第 X+1 个人不能配对
      那么如果查到下一个人可以和前面的配对,那么就可以新增加两个人



#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
    int X;
    while(cin>>X)
    {
        string str;
        cin>>str;
        int len=str.size();
        int m=0,w=0;
        int flag=1,mins;
        for(int i=0; i<len; i++)
        {
            if(str[i]=='M')m++;
            if(str[i]=='W')w++;
            if(m>w)mins=w;
            else mins=m;
            if(fabs(w-m)>(X+1))
            {
                cout<<i<<"--"<<endl;
                flag=0;
                break;
            }
        }
        if(flag==1&&fabs(w-m)<=X)// 如果没有标记,全部排队的人数也合法:男女之差不超过 X
            cout<<len<<endl;
        else
            cout<<(mins*2+X)<<endl;
    }
    return 0;
}

0 0
原创粉丝点击