Vova and Trophies _Codeforces 1082B

题目

 

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.

 题目大意

 vova有一大堆金牌和银牌,随机拜访,但是他想做一次交换:交换一块金牌和一块银牌的位置,问你交换至多一次后,连续的金牌数最多有多少

算法:模拟

代码 

#include<bits\stdc++.h>
using namespace std;
int b[100010]; 
int main()
{
	int n,i=0,j=0;
	cin>>n;getchar();
	char c;
	while((c=getchar())!='\n')
	{
		if(c=='S') b[j++]=i;
		i++;
	}
	int max=0;
	if(j==0) max=i;//防止全部都是G的情况 
	if(b[1]-1>=max) //交换第一个S 
	{
		if(b[1]-1 < n-j) max=b[1];
		else max=b[1]-1;
	} 
	
	if(n-b[j-2]-2>=max) //交换最后一个S 
	{
		if(n-b[j-2]-2 < n-j) max=n-b[j-2]-1;
		else max=n-b[j-2]-2;
	}
	
	for(int k=0;k<j-1;k++)
		if(b[k+1]-b[k]-1>=max) max=b[k+1]-b[k]-1;
	
	for(int k=1;k<j-1;k++)
		if(b[k+1]-b[k-1]-2>=max) 
		{
			if(b[k+1]-b[k-1]-2 < n-j) max=b[k+1]-b[k-1]-1;
			else max=b[k+1]-b[k-1]-2;
		}
	if(n-j==0) cout<<"0";
//	else if(n-j==1) cout<<"1";
	else cout<<max;
	return 0;
}

 分析

 给定一串字符,每次都记录s的位置,两个相邻s的位置差-1就是一串连续的金牌数(至于为什么-1:5和7之间只有一个6,7-5=2)

举个例子: 第五个s 第六个s 第七个s... 如字符串:   S4GGGS5GGS6GGGS7GGS8S9

如果把S6换成金牌,那么新的连续g区间就是S6,S5之间的g数量+ S7,S6数量+1

这个时候要想一想了 如果是 SGGGSGGS这种情况 就不能+1了

总起来说就是,要比较S7,S6之间的g数量+ S6,S5之间的g数量是否等于全部的g数量(如果等于,说明别的地方没有g了他,也就只能拿自身的g来替换s了,那么就不能+1;其他情况就说明了其他地方还有g可以替换此处的s,理所当然的+1) 

也就是

1:如果某个s前后的g的个数总和小于总共g的个数,说明可以从其他地方拿一个,这样的话就可以将s前后的两个g串合起来然后长度+1;

2:如果某个s前后的g的个数总和等于总共g的个数(比如ggggsggg)那么就是将一个串中拿出一个g替换s,这样长度就是两个g串长度的和

上一段代码就是这么做的,但是b数组是从0开始的,需要单独考虑第一个s和最后一个s。

因此想到了一种方法,让b数组从1开始,b[0]用来存放-1,就是假装第一个s之前还有一个s,这个s位置是-1;假装最后一个s之后还有一个s,这个s是n

还是蛮好理解的,比如GSGGSGG,那么加上这两个sGSGGSGGs,这样就可以一起放到for循环计算,不用单独拿出第一个s和最后一个s来特殊考虑了

代码如下

#include<bits\stdc++.h>
using namespace std;
int b[100010]; 
int main()
{
	int n,i=0,j=1;
	cin>>n;getchar();
	char c;
	while((c=getchar())!='\n')
	{
		if(c=='S') b[j++]=i;
		i++;
	}
	int num=n-j+1;//num表示G的个数 j-1表示s个数 
	int max=0;
	b[0]=-1;b[j]=n; 
	if(n==num) max=i;//全部都是G的情况 
	for(int k=1;k<j;k++)
		if(b[k+1]-b[k-1]-2>=max) 
		{
			if(b[k+1]-b[k-1]-2 < num) max=b[k+1]-b[k-1]-1;
			else max=b[k+1]-b[k-1]-2;
		}
		
	if(num==0) cout<<"0";
	else cout<<max;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cherish_lii

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

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

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

打赏作者

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

抵扣说明:

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

余额充值