CF_353D (queue)
  W79oLdECuAdO 2023年11月02日 25 0


题意:

在一个由男生和女生组成的队中,每一秒如果一个男生在一个女生的前面,

他们就会互换位置。求女生全换到最前面用的最少时间。


DP转移方程: ans=max(ans+1,pre); pre是从后往前到当前位置时的女生数

        意思是在当前如果是男生并且该 男生后面有女生的存在,则选取



#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;  



int main()
{
	freopen("in.txt","r",stdin);
	int i,j,k;
	string s;
	cin>>s;
	
	int ans=0;
	int pre=0;
	for (i=s.size()-1;i>=0;i--)
	{
		if (s[i]=='F')
			pre++;
		else  if (pre)
			ans=max(ans+1,pre);
	}
	
	cout<<ans<<endl;
	
	
	return 0;
}







D. Queue



time limit per test



memory limit per test



input



output



n

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.



Input



s1s2... sn (1 ≤ n ≤ 106), consisting of capital English letters M and F. If lettersi equals M, that means that initially, the line had a boy on the i-th position. If letter si equals F, then initially the line had a girl on the i-th position.



Output



0.



Sample test(s)



input



MFM



output



1



input



MMFF



output



3



input



FFMMM



output



0



Note



MFM  →  FMM.

MMFF  →  MFMF  →  FMFM  → FFMM.





【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年11月22日   22   0   0 linuxApacheci
  jnZtF7Co41Wg   2023年11月24日   30   0   0 分区表cicentos
  py5aPqzocVnd   2023年11月22日   37   0   0 协议ci