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)/~