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.

在左右两侧都是G,中间为S的时候,在最后需要和直接连续G的长度交换一个S为G的比较一下,wa在这里了。

代码:

 //B
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f; char c[maxn];
vector<int> ve; int main()
{
int n;cin>>n;
cin>>c;
int num=;
for(int i=;i<n;i++){
if(c[i]=='G') num++;
if(c[i-]=='G'&&c[i]=='S'&&c[i+]=='G') ve.push_back(i);
}
int ret=,tmp=;
for(int i=;i<n;i++){
if(c[i]=='G') tmp++;
else ret=max(ret,tmp),tmp=;
}
ret=max(ret,tmp);
ret=min(ret+,num);
if(num==n) {cout<<n<<endl;return ;}
if(num==) {cout<<<<endl;return ;}
if(ve.size()==){
int ans=-,cnt=;
for(int i=;i<n;i++){
if(c[i]=='G') cnt++;
if(c[i]=='S') ans=max(ans,cnt),cnt=;
}
ans=max(ans,cnt);
cout<<min(num,ans+)<<endl;
}
else{
int ans=,flag=;
for(auto it:ve){
int cnt=;
for(int i=it-;i>=;i--){
if(c[i]=='S') break;
else cnt++;
}
for(int i=it+;i<n;i++){
if(c[i]=='S') break;
else cnt++;
}
ans=max(ans,cnt);
}
ans=min(num,ans+);
cout<<max(ans,ret)<<endl;
}
} /*
16
GSGSSGSSGGGSSSGS 4
*/

Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies (贪心+字符串)

    B. Vova and Trophies time limit per test2 seconds memory limit per test256 megabytes inputstandard i ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) Solution

    A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...

  4. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies

    传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你 ...

  5. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

    D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  9. Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

    传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...

随机推荐

  1. C11性能之道:标准库优化

    1.emplace_back减少内存拷贝和移动 emplace_back能通过参数构造对象,不需要拷贝或者移动内存,相比pusk_back能更好的避免内存的拷贝和移动,使容器插入元素性能得到进一步提升 ...

  2. Maven -- 在进行war打包时用正式环境的配置覆盖开发环境的配置

    我们的配置文件一般都放在  src/main/resource 目录下. 假定我们的正式环境配置放在 src/main/online-resource 目录下. 那么打成war包时,我们希望用onli ...

  3. Java面试题整理二

    一.io有几种流? 有两种流:字节流和字符流. 字节流继承自inputstream和outstream. 字符流继承自write和read. 二.Jdbc的开发流程? JDBC编程的六个步骤: 1)加 ...

  4. 【bzoj3387-跨栏训练】线段树+dp

    我们可以想到一个dp方程:f[i][0]表示当前在i个栅栏的左端点,f[i][1]表示在右端点. 分两种情况: 第一种:假设现在要更新线段gh的左端点g,而它下来的路径被ef挡住了,那么必定是有ef来 ...

  5. 【BZOJ】1914: [Usaco2010 OPen]Triangle Counting 数三角形

    [题意]给定坐标系上n个点,求能构成的包含原点的三角形个数,n<=10^5. [算法]极角排序 [题解]补集思想,三角形个数为C(n,3)-不含原点三角形. 将所有点极角排序. 对于一个点和原点 ...

  6. CSUST选拔赛题解

    本鶸鸡于本月10号参加了蔽校的选拔赛,成绩差的死,大部分的题都是赛后花了好长时间才补出来的,其中有些题还是靠QAQorz大佬帮忙才能解决,感谢Qls对我的帮助~接下来就附带上我的暴力题解,大佬们有更好 ...

  7. 换行符 \r \n \r\n 在不同系统下的区别

    '\r'是回车,前者使光标到行首,(carriage return)'\n'是换行,后者使光标下移一格,(line feed)\r 是回车,return\n 是换行,newline对于换行这个动作,u ...

  8. CSS3 动画实现方法大全

    常用效果总结(需要引用animate.css) <!doctype html> <html lang="en"> <head> <meta ...

  9. js_layer弹窗的使用和总结

    2018-04-10 一张呈现给用户的网页,会有很多种交互,比如连不上网络,用户点击按钮时向后台请求数据不成功等等.像这些情况,用户是看不见的, 要给用户更好的体验,在特定的时间,给客户反馈内容.实时 ...

  10. javascript执行上下文学习一

    原文: http://web.jobbole.com/84044/ http://blog.csdn.net/github_34514750/article/details/52901781 1.三种 ...