CF353D.Queue

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are n schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.

Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the i-th position has a boy and the (i + 1)-th position has a girl, then in a second, the i-th position will have a girl and the (i + 1)-th one will have a boy.

Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.

Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing.

有 nn 名学生(男生和女生)排在校内面包摊前。此时面包尚未做好,队伍正在发生一些变化。

每一秒,所有正站在女生前面的男生会同时与身后的女生交换位置(从而使女生更靠近队伍前端)。换句话说,如果在某一时刻,第 ii 个位置是男生,而第 i+1i+1 个位置是女生,那么一秒钟后,第 ii 个位置将变为女生,第 i+1i+1 个位置将变为男生。

我们来看一个由四人组成的队伍示例:从队首到队尾依次为——男生、男生、女生、女生。下一秒,队伍变为:男生、女生、男生、女生;再下一秒变为:女生、男生、女生、男生;再下一秒变为:女生、女生、男生、男生。此后队伍将不再发生变化。

你的任务是:给定队伍中学生的初始排列,求出使所有女生都排到所有男生前面所需的时间(上述示例中耗时为 3 秒)。由于烘烤面包耗时很长,因此在队伍停止变化之前,无人离开队伍。

输入格式

The first line contains a sequence of letters without spaces _s_1_s_2... s__n (1 ≤ n ≤ 106), consisting of capital English letters M and F. If letter s__i equals M, that means that initially, the line had a boy on the i-th position. If letter s__i equals F, then initially the line had a girl on the i-th position.

第一行包含一个无空格的字母序列 s1s2…sns_1s_2\ldots s_n(1 ≤ n ≤ 1061 \leq n \leq 10^6),其中仅由大写英文字母 M 和 F 组成。若字母 sis_i 为 M,则表示初始时第 ii 个位置上站了一名男孩;若字母 sis_i 为 F,则表示初始时第 ii 个位置上站了一名女孩。

输出格式

Print a single integer — the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.

输出一个整数——将队列中所有女孩移到男孩前面所需的秒数。如果队列中只有男孩或只有女孩,则输出 0。

输入输出样例

  • 输入#1

    MFM

    输出#1

    1
  • 输入#2

    MMFF

    输出#2

    3
  • 输入#3

    FFMMM

    输出#3

    0

说明/提示

In the first test case the sequence of changes looks as follows: MFM  →  FMM.

The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF  →  MFMF  →  FMFM  →  FFMM.

第一个测试用例中,变化序列为:MFM → FMM。

第二个测试样例对应题目描述中的样例。变化序列为:MMFF → MFMF → FMFM → FFMM。

输入解题思路,AI测评打分。不知道怎么写?

首页