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.

当时做题的时候交了一发过了,不过最后竟然被hack了,哎,还是太弱了,要自闭了。嘤嘤嘤;

题意很简单,就不说了,

思路:

1、我的思路:找一下连续的G串有多少串sumg并记录最大值maxx1,然后再找  如果一个S的左边和右边都是G串那么再记录一下此类串的最大长度maxx2

如果sumg==1输出maxx1

如果sumg==2输出max(maxx1+1,maxx2);

如果sumg>2输出max(maxx1+1,maxx2+1);

2、大佬的思路:

记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 'G' 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen

以下是鄙人的代码,很繁琐;

下面还有大佬的代码;

 #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+;
const int INF=0x3f3f3f3f;
char a[maxn];
int main()
{
int n,sumg=, tempg=;
scanf("%d",&n);
getchar();
scanf("%s",a);
int maxx1=,maxx2=,temps=,flag=,CNT=;
for(int i=;i<n;i++)
{
if(a[i]=='G')
{
sumg++;
tempg++;
if(i==n-)
{
if(temps)
maxx1=max(maxx1,temps+tempg);
else
maxx2=max(maxx2,tempg);
CNT++;
}
}
else
{
if(tempg)CNT++;
if(flag==)
{
maxx1=max(tempg+temps,maxx1);
temps=tempg;
tempg=;
flag=;
}
if(a[i+]!='G')
{ maxx2=max(maxx2,tempg);
tempg=;
temps=;
}
else
{
if(tempg&&a[i-]=='G')
flag=;
temps+=tempg;
tempg=;
}
}
}
if(sumg==n)
printf("%d\n",n);
else if(sumg)
{
if(CNT==)
{
printf("%d\n",max(maxx1,maxx2+));
}
else if(CNT==)
{
printf("%d\n",maxx2);
}
else
{
printf("%d\n",max(maxx1+,maxx2+));
}
}
else
printf("0\n");
return ;
}

一下是大佬的代码:

 #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+;
const int INF=0x3f3f3f3f;
string a;
int main()
{
int n,ans=,temps=,sumg=,tempg=;
cin>>n;
cin>>a;
for(int i=;i<n;i++)
{
if(a[i]=='G')
{
sumg++;
tempg++;
}
else
{
temps=tempg;
tempg=;
}
ans=max(ans,tempg+temps+);
}
ans=min(ans,sumg);
printf("%d",ans);
return ;
}

Vova and Trophies CodeForces - 1082B(思维题)的更多相关文章

  1. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  2. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  3. CodeForces - 417A(思维题)

    Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit  ...

  4. B. Vova and Trophies 字符串预处理+思维+贪心

    题意:给出一个字符串 只有G和S  可以交换任意两个位置的字符一次 问 最长的G的长度是多少 思路:预处理字符串 把相同的G粘成一个G 记一下数量  字符串变为 GSSGSGGSGSSG 相邻有一个S ...

  5. B - Sonya and Exhibition CodeForces - 1004B (思维题)

    B. Sonya and Exhibition time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. CF1082B Vova and Trophies 题解

    CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...

  7. Codeforces 1082B Vova and Trophies 模拟,水题,坑 B

    Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...

  8. Codeforces 1082B Vova and Trophies(前缀+后缀)

    题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...

  9. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】

    传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...

随机推荐

  1. NSDate 格式化 及 互转

    /* NSDateFormatter的作用 1.NSString -> NSDate 2.NSDate -> NSString */ void fmt_date_to_string(); ...

  2. oracle11g 手工建库步骤

    #create oracle instance parameter vi initkevin.or db_name='kevin' memory_target=0 sga_max_size=5G sg ...

  3. jquery 实现可编辑div

    html大致例如以下: <ol id="ol_group" class="list-group list_of_items"> <li cla ...

  4. Xamarin nuget package update 错误

    update xamarin.Forms包时出现错误: 'The specified path, file name, or both are too long. The fully qualifie ...

  5. iOS_截屏并裁剪

    截图使用场景: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fil ...

  6. 万维网 WWW (World Wide Web)

    万维网 WWW (World Wide Web)并非某种特殊的计算机网络.万维网是一个大规模的.联机式的信息储藏所.万维网用链接的方法能非常方便地从因特网上的一个站点访问另一个站点,从而主动地按需获取 ...

  7. 将分布式-队列的实现交给redis

    import requestsimport reimport timefrom redis import Redisimport threading REDIS_HOST, REDIS_PORT, P ...

  8. js【面向过程编程】、好、 【init()、 GetData()、 bindData()、bindDom、 bindEvent()、buyProduct()、AddProductToCart()】*****************

    1. 一般页面开发方式 [可读性差.可维护性差]------初级开发工程师 一般页面编写方法 var name = 'iphone8' var description = '手机中的战斗机 ' var ...

  9. bzoj2242 [SDOI2011]计算器——BSGS

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一次写BSGS,参考了好多好多博客: 然而看到的讲解和模板是一种写法,这道题的网上题 ...

  10. Java常用的数组排序算法(面试宝典)

    这段时间有些忙,今天空闲出来给大家分享下Java中常用的数组排序算,有冒泡排序.快速排序.选择排序.插入排序.希尔算法.并归排序算法.堆排序算法,以上排序算法中,前面几种相对后面的比较容易理解一些.下 ...