0
点赞
收藏
分享

微信扫一扫

CodeForces - 1082B . Vova and Trophies


B. Vova and Trophies

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

10
GGGSGGGSGG

output

Copy

7

input

Copy

4
GGGG

output

Copy

4

input

Copy

3
SSS

output

Copy

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个字符,只有G S, 任意交换一次,使最大连续G,求最大数量。

 

分析:

这题不难,虽让坑了我好长时间

直接统计一下连续的G的数量就行,我比赛时候用vector保存的,但其实不需要用,直接开两个变量保存就行,然后,加一个判断就是只有这两段相差为一的时候,才能加在一起,不然只能加一,这里需要有一种特殊情况,就是GGGG或者GGGSGGGG,就是交换的时候没有S或者没有G了,这里统计一下G的总数量sum,min(ans,sum)即可

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100000+5
#define rep(i,n) for(int i=0;i<n;i++)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pd(n) scanf("%d\n",n)
#define pll(n) scanf("%lld\n",n)
#define MAX 26
typedef long long ll;
const ll mod=1e9+7;
ll n,m;
ll a[N];
ll b[N];
int main()
{
//string s;
//cin>>s;
scanf("%lld",&n);
//getchar();
string s;
cin>>s;
ll num=0;
ll maxx=0;
vector<ll>p;
p.clear();
ll sum=0;
for(int i=0;i<n;i++)
{
if(s[i]=='G')
{
num++;
sum++;
}
else
{
p.push_back(num);
maxx=max(maxx,num);
num=0;
}
if(i==n-1)
{
p.push_back(num);
maxx=max(maxx,num);
}
}
if(maxx==0)
{
cout<<0<<endl;
return 0;
}
if(maxx!=0)
maxx+=1;

int m;

for(int i=0;i<(p.size()-1);i++)
{

if(p[i]>0&&p[i+1]>0)
{
maxx=max(p[i]+p[i+1]+1,maxx);
}

}
printf("%lld\n",min(sum,maxx));


return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
string a;
int main()
{
int n,ans=0,temps=0,sumg=0,tempg=0;
cin>>n;
cin>>a;
for(int i=0;i<n;i++)
{
if(a[i]=='G')
{
sumg++;
tempg++;
}
else
{
temps=tempg;
tempg=0;
}
ans=max(ans,tempg+temps+1);
}
ans=min(ans,sumg);
printf("%d",ans);
return 0;
}

 

举报

相关推荐

0 条评论