CodeForces 1082B

H-世

https://vjudge.net/problem/2065317/origin

Vova has won n 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 n (2≤n≤105) — the number of trophies.

The second line contains n characters, each of them is either G or S. If the i-th character is G, then the i-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

Note

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




题目大意

一水题,就是题目给出一串字符,只有G和S,只能交换一次任意两个字符,问交换之后的字符串之中G的最长的不中断的字符串是多长。

//最初的代码,我将所有的情况都算出之后在求最大,比较复杂,细节有点头大,不推荐使用
#include<stdio.h>
#include<string.h>
int n,k,index,sum,b[100005],c[100005];
char a[100005];
int MAX(int a,int b)
{
	return a>b?a:b;
}

int main()
{
	while(~scanf("%d",&n))
	{
		k=sum=index=0;
		getchar();
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		for(int i=0;i<n;i++)
		{
			scanf("%c",&a[i]);
			if(a[i]=='G') {
				if(a[i-1]=='S') k++;
				b[k]++;
			}
			else if(a[i]=='S')
				k++;
			if(a[i]=='S'&&i==n-1) k++;
		}
		if(b[k]>0) k++;
		int ans=0;
		for(int i=0;i<k;i++)
		{
			if(b[i]>0) sum++;
		}
		for(int i=0;i<k;i++)
		{
			if(b[i]>0) {
				if(i+2<k) {
					i+=2;
					if(b[i]==0) {
						if(sum>1) 
							c[index]=b[i-2]+1;
						else 
							c[index]=b[i-2];
					}
					else {
						if(sum>2) 
							c[index]=b[i-2]+b[i]+1;
						else 
							c[index]=b[i-2]+b[i];
					}
					i-=2;
				}
				if(i-2>=0) {
					i=i-2;
					if(b[i]==0) {
						if(sum>1) 
							c[index]=MAX(b[i+2]+1,c[index]);
						else 
							c[index]=MAX(b[i+2],c[index]);
					}
					else {
						if(sum>2) 
							c[index]=MAX(b[i+2]+b[i]+1,c[index]);
						else 
							c[index]=MAX(b[i+2]+b[i],c[index]);
					}
					i+=2;
				}
				if(i+2>=k&&i-2<0) c[index]=b[i];
			}
			index++;
		}
		for(int i=0;i<index;i++)
		{
			if(c[i]>ans) ans=c[i];
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值