【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

163 篇文章 1 订阅
98 篇文章 1 订阅

题干:

Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.

The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.

Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

Input

The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of trophies.

The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the ii-th trophy is a golden one, otherwise it's a silver trophy.

Output

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

Examples

Input

10
GGGSGGGSGG

Output

7

Input

4
GGGG

Output

4

Input

3
SSS

Output

0

Note

In the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 77.

In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44.

In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 00.

题目大意:

    给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换。求连续G的最长长度。

解题报告:

   深夜掉分2333、、、(C明显**题啊咋就没敢写,,大概还是AB用的时间太长了,还是苔菜)

   这题看似很好实现但是其实还是需要进行一些思维的转化才能做的。用了一堆数组,L和R数组分别表示截止当前的前缀和后缀中是否出现过G。pre代表截止当前的连续G串,hou代表截止当前的后缀G串。S数组代表字符S出现过的位置的记录。

  数组设定好之后就可以实现了。。首先啊,,答案肯定由两种情况组成(其中取个最大值就可以了)。一种是连续G串。一种是一段G串中间带个S。对于第一种比较好处理。对于第二种,又分两种情况,一种是还有多余的G可以填补这个S,一种是没有多余的G了,,只能用两边的一个G来填补这个S。。所以代码就出来了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int pre[MAX],hou[MAX];
int S[MAX];
bool L[MAX],R[MAX];
int main()
{
	int n,cnt=0,tot=0;
	cin>>n;
	cin>>(s+1);
	for(int i = 1; i<=n; i++) {
		if(s[i] == 'G' || L[i-1] == 1) L[i] =1;
		else L[i]=0;
	}
	for(int i = n; i>=1; i--) {
		if(s[i] == 'G' || R[i+1] == 1) R[i] = 1;
		else R[i] = 0;
	}
	for(int i = 1; i<=n; i++) {
		if(s[i] == 'G') cnt++;
		else S[++tot] = i;
	}
	for(int i = n; i>=1; i--) {
		if(s[i] == 'G') hou[i] = hou[i+1] + 1;
		else hou[i] = 0;
	}
	int maxx = -1,tmp=0;
	for(int i = 1; i<=n; i++) {
		if(s[i] == 'G') pre[i] = pre[i-1]+1,tmp++;
		else {
			pre[i]=0;
			maxx = max(maxx,tmp);
			tmp=0;
		}
	}
	maxx = max(tmp,maxx);
	int ma = 0,flag = 0;//flag=0代表没有使用过S 
	tmp=0;
	int tmp1 = 0;
	for(int i = 1; i<=tot; i++) {
		int cur = pre[S[i]-1] + hou[S[i]+1];
		if(cur < cnt && (L[S[i]-1] || R[S[i]+1])) ma = max(ma,cur+1);
		if(cur == cnt) ma = max(ma,cur);
	}
	
//	for(int i = 1; i<=n; i++) {
//		if(flag == 1 && s[i] == 'S') {
//			ma = max(ma,tmp);tmp=tmp1;flag=0;
//		}
//		if(s[i] == 'G') tmp++,tmp1++;
//		else if (s[i] == 'S') {
//			flag=1;tmp1 = 0;
//			tmp++;
//		}
//	}
//	ma = max(ma,tmp);
//	if(cnt <= ma) ma=-1; 
	printf("%d\n",max(maxx,ma));
	return 0 ;
 }
/*
5
SGGSG

5
GGGSG

总结:

   看连续G的个数的时候一定别忘了还有这一套那就是执行完了之后maxx = max(tmp,maxx),,因为有可能进行到末尾了,,这个tmp还没赋值给maxx。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值