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. 【UI自动化方面】

    1.自动化执行失败如何排查故障. 答:1).查看log,错误原因.[log不详细的话,可以优化] 2).排查是否真的有bug,若不是bug查看是否是新版本引入了新的变更. 3).调试脚本看自己脚本是不 ...

  2. The Pragmatic Programmer 读书笔记之中的一个 DRY-Don’t Repeat Youself

     The Pragmatic Programmer读书笔记之中的一个 DRY-Don't Repeat Youself 尽管自己买了非常多软件project方面的书,可是由于时间的问题.一直没有静 ...

  3. Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式

    C. Dreamoon and Sums   Dreamoon loves summing up something for no reason. One day he obtains two int ...

  4. mysql20170410练习代码+笔记

    今天的几道高级sql查询真的挺难的,感觉好像视频里讲过,当时也没有练,已经淡化了很多,sql还是要多练习啊!确实逻辑性挺强的. SELECT studentResult,studentNO FROM ...

  5. [NHibernate] 入门实例 NHibernate 3.3 GA + VS2010 +MySQL

    题外话: 中国的技术论坛要赶上stackoverflow的水平.至少还需35年.中国程序猿笔者的特点是:太浮躁,太easy下总结.太自得其乐,虽说写的是为了让别人更好的看.却也演变成了一种" ...

  6. mkisofs

    createrepo -g /enp/comps.xml . yum -y --downloadonly --downloaddir=/enp/Packages upgrade mkisofs -o ...

  7. 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。

    <script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...

  8. null, undefined,"",0,false是什么关系?

    null本质上和0,"",false是一类东西,它们都表示一种数据类型的非值.正如0表示数字类型的非值,""表示字符类型的非值一样,null表示完全空的对象,即 ...

  9. NetCore Netty 框架 BT.Netty.RPC 系列随讲 二 WHO AM I 之 NETTY/NETTY 与 网络通讯 IO 模型之关系?

    一:NETTY 是什么? Netty 是什么?  这个问题其实百度上一搜一堆. 这是官方话的描述:Netty 是一个基于NIO的客户.服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个 ...

  10. android 二维码 扫描,生成,竖屏

    最近公司有用到二维码,生成,扫描,所以学习了一下,和大家分享: demo 见下面链接,已经改成竖屏: http://download.csdn.net/detail/q610098308/868101 ...