题干:

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.

题目大意:

给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换。求连续G的最长长度。

解题报告:

深夜掉分2333、、、(C明显**题啊咋就没敢写,,大概还是AB用的时间太长了,还是苔菜)

这题看似很好实现但是其实还是需要进行一些思维的转化才能做的。用了一堆数组,L和R数组分别表示截止当前的前缀和后缀中是否出现过G。pre代表截止当前的连续G串,hou代表截止当前的后缀G串。S数组代表字符S出现过的位置的记录。

数组设定好之后就可以实现了。。首先啊,,答案肯定由两种情况组成(其中取个最大值就可以了)。一种是连续G串。一种是一段G串中间带个S。对于第一种比较好处理。对于第二种,又分两种情况,一种是还有多余的G可以填补这个S,一种是没有多余的G了,,只能用两边的一个G来填补这个S。。所以代码就出来了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int pre[MAX],hou[MAX];
int S[MAX];
bool L[MAX],R[MAX];
int main()
{int n,cnt=0,tot=0;cin>>n;cin>>(s+1);for(int i = 1; i<=n; i++) {if(s[i] == 'G' || L[i-1] == 1) L[i] =1;else L[i]=0;}for(int i = n; i>=1; i--) {if(s[i] == 'G' || R[i+1] == 1) R[i] = 1;else R[i] = 0;}for(int i = 1; i<=n; i++) {if(s[i] == 'G') cnt++;else S[++tot] = i;}for(int i = n; i>=1; i--) {if(s[i] == 'G') hou[i] = hou[i+1] + 1;else hou[i] = 0;}int maxx = -1,tmp=0;for(int i = 1; i<=n; i++) {if(s[i] == 'G') pre[i] = pre[i-1]+1,tmp++;else {pre[i]=0;maxx = max(maxx,tmp);tmp=0;}}maxx = max(tmp,maxx);int ma = 0,flag = 0;//flag=0代表没有使用过S tmp=0;int tmp1 = 0;for(int i = 1; i<=tot; i++) {int cur = pre[S[i]-1] + hou[S[i]+1];if(cur < cnt && (L[S[i]-1] || R[S[i]+1])) ma = max(ma,cur+1);if(cur == cnt) ma = max(ma,cur);}//   for(int i = 1; i<=n; i++) {
//      if(flag == 1 && s[i] == 'S') {
//          ma = max(ma,tmp);tmp=tmp1;flag=0;
//      }
//      if(s[i] == 'G') tmp++,tmp1++;
//      else if (s[i] == 'S') {
//          flag=1;tmp1 = 0;
//          tmp++;
//      }
//  }
//  ma = max(ma,tmp);
//  if(cnt <= ma) ma=-1; printf("%d\n",max(maxx,ma));return 0 ;}
/*
5
SGGSG5
GGGSG

总结:

看连续G的个数的时候一定别忘了还有这一套那就是执行完了之后maxx = max(tmp,maxx),,因为有可能进行到末尾了,,这个tmp还没赋值给maxx。。

【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)相关推荐

  1. CodeForces - 1082B Vova and Trophies

    传送 题意:一个只包含G和S的字符串,一次交换能使连续的G最长为多少. 思路:想要使只包含G的子串最长,显然贪心地将两个子串中间的S与一个不在子串的G交换再与最大值比较就可以了,要注意除了这两个子串外 ...

  2. CF 1082B Vova and Trophies

    题目:Vova and Trophies 思路: 如果有两块G中间只隔一个S,就把它和一个G交换. 否则就移一个S到最长的那一块旁边. 代码: #include<bits/stdc++.h> ...

  3. CodeForces - 1362E Johnny and Grandmaster(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个基数 p ,再给出 n 个指数 k ,换句话说,现在有一个长度为 n 的序列,每个元素都是 p^k[ i ] ,现在需要将这个序列分到两个集合中,使得两个集合元素 ...

  4. CodeForces - 1303D Fill The Bag(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个背包,容量为 k ,再给出 n 个物品,每个物品的大小保证是 2 的幂次,现在可以进行操作,使得一个物品分为大小相等的,且大小等于原物品一半的两个物品,比如一个物 ...

  5. CodeForces - 946E Largest Beautiful Number(贪心+模拟)

    题目链接:点击查看 题目大意:给出一个数位长度为偶数的数字 n,需要求出一个比 n 小的,且所有数位重新排列后可以形成回文串,要求这个数字尽可能大 题目分析:从最低位开始贪心,依次枚举每一位的数字,然 ...

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

  7. 校内hu测(10.6T2,T3)(乱搞+贪心+模拟)

    @liu_runda T2.便(then) [题目描述] 给出一个R*C的棋盘.共有R行C列,R*C个格子.现要在每个格子都填一个非负整数.使得任意一个2*2的正方形区域都满足这样的性质:左上角的数字 ...

  8. 【NOIP2013】积木大赛(差分数组,贪心模拟)

    题目 原题链接 问题描述 分析 直观思路--贪心模拟:每次都处理最长正整数区段. 以[2,3,4,1,2][2,3,4,1,2][2,3,4,1,2]为例: [2,3,4,1,2]⟹[1,2,3,0, ...

  9. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  10. Codeforces Round #249 (Div. 2) (模拟)

    Codeforces Round #249 (Div. 2) (模拟) C. Cardiogram time limit per test 1 second memory limit per test ...

最新文章

  1. 安装部署Spark 1.x Standalone模式集群
  2. 使用 OpenMVG+PMVS实现视觉三维重建
  3. ffmpeg avformat_open_input always returns “Protocol not found”
  4. 拾趣——ios::sync_with_stdio(false)详解(提高代码运算速度)
  5. cocos2dx3.0五种屏幕适配模式,及FIXED_WIDTH、FIXED_HEIGHT使用
  6. JVM快速调优手册v1.0
  7. Python unittest –单元测试示例
  8. 谷歌Chrome浏览器开发者工具教程—基础功能篇
  9. 毕设题目:Matlab优化充电
  10. android配置网络权限管理,Android 网络权限配置
  11. 周立功bms汽车锂电池管理系统解决方案
  12. 相位一致性的基本原理及应用问题
  13. 房地产估值法研究报告_房地产估值方法
  14. PhoneWindowManager().interceptKeyBeforeQueueing()中的interactive变量值的来源
  15. 美团商品平台化之路—关于架构原则的思考
  16. 浏览器存储的方式有哪些
  17. linux 软链接创建及拷贝
  18. ansible dnf模块详解
  19. 如何使用IDEA创建一个新的项目
  20. python空间数据处理_基于Python的空间数据批量处理方法

热门文章

  1. python判断字母数字_Python判断字符串是否为字母或者数字(浮点数)的多种方法
  2. C#中变量(成员变量、局部变量、全局变量)的作用域
  3. bootstrap带有下拉按钮的输入框_关于bootstrap--表单(下拉select、输入框input、文本域textare复选框checkbox和单选按钮radio)...
  4. python返回函数值并退出函数_如何在python的阻塞的函数中获取变量值
  5. cc2530i2c可同时接受两个传感器的数据吗_汽车方向及维修_玉树沃尔沃S40方向机,宝马531电子方向机进水可以维修吗...
  6. 法线贴图Nomal mapping 原理
  7. POLLERR的故事
  8. java fx 建立窗体,3花式窗体与JavaFX CSS
  9. python迭代器创建序列_Python 中迭代器与生成器实例详解
  10. 【转】Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现