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. hdu 1728 逃离迷宫 bfs记步数

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  2. BeautifulSoup中各种html解析器的比較及使用

    Beautiful Soup解析器比較 ·Beautiful Soup支持各种html解析器.包含python自带的标准库.还有其它的很多第三方库模块. 当中一个就是lxml parser,至于lxm ...

  3. restrict关键字

    值得注意的是,一旦你决定使用restrict来修饰指针,你必须得保证它们之间不会互相重叠,编译器不会替你检查. 关键字restrict有两个读者.一个是编译器,它告诉编译器可以自由地做一些有关优化的假 ...

  4. P5180 【模板】支配树

    这个题乱七八糟的,和之前的灭绝树有点像,但是不一样.那个是DAG,这个是有向图.简单步骤就是先求出来dfs序,然后求出半支配点(?),然后通过这个求支配点. 算法不是很理解,先放在这. 题干: 题目背 ...

  5. HDU4336:Card Collector

    题意 有n张卡片,每一次 有pi的概率买到第i张卡.求买到所有卡的期望购买次数. n<=20 解析 Solution 1:大力状压(就是步数除以方案数) #include<iostream ...

  6. bzoj2142 礼物——扩展卢卡斯定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 前几天学了扩展卢卡斯定理,今天来磕模板! 这道题式子挺好推的(连我都自己推出来了) , ...

  7. JSP-Runoob:JSP 链接数据库

    ylbtech-JSP-Runoob:JSP 链接数据库 1.返回顶部 1. JSP 连接数据库 本教程假定您已经了解了 JDBC 应用程序的工作方式.在您开始学习 JSP 数据库访问之前,请访问 J ...

  8. 66.extjs 里对getvalue() 和getRawValue()

    转自:https://blog.csdn.net/u014236541/article/details/49663589?locationNum=8

  9. Hadoop - WordCount代码示例

    文章来源:http://www.itnose.net/detail/6197823.html import java.io.IOException; import java.util.Iterator ...

  10. cogs750栅格网络流(最小割)

    750. 栅格网络流 ★★☆   输入文件:flowa.in   输出文件:flowa.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太 ...