Vova and Trophies(思维)

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.

emmmm,第一个写的就是这个题目,意思不难,但是就是情况多了,一开始写完了改了好久才对了,然后提交一发过。嘻嘻,我看着网上用到了vector,不清楚为啥这么用。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxx=2e5+10;
int a[maxx];
char s;
int n;

void init()
{
	memset(a,0,sizeof(a));
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		getchar();
		init();
		int cnt=0;
		int ant=0;
		int gcnt=0;
		int flag=0;
		for(int i=0;i<n;i++)
		{
			scanf("%c",&s);
			if(s=='G') flag=1,cnt++,gcnt++;
			else if(flag)
			{
				a[ant++]=cnt;
				cnt=0;
			}
			if(i==n-1&&s=='G') a[ant++]=cnt;
		}
		if(ant==0)//SSSSS这种情况
		{
			cout<<0<<endl;
			continue;
		}
		if(ant==1)//GGGG或者SGGGG或者GGGGS这三种情况
		{
			cout<<max(cnt,a[0])<<endl;//改来改去发现这样才对
			continue;
		}
		int maxx=-1;
		for(int i=0;i<ant;i++)
		{
			if(a[i]+a[i+1]>=gcnt)
			maxx=max(maxx,a[i]+a[i+1]);//GGGGSGG这种情况
			else maxx=max(maxx,a[i]+a[i+1]+1);//GGGSSGGGSSGG这种情况
		}
		printf("%d\n",maxx-1+1);
	}
} 

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值