题目

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.

题目大意

vova有一大堆金牌和银牌,随机拜访,但是他想做一次交换:交换一块金牌和一块银牌的位置,问你交换至多一次后,连续的金牌数最多有多少

算法:模拟

代码

#include<bits\stdc++.h>
using namespace std;
int b[100010];
int main()
{int n,i=0,j=0;cin>>n;getchar();char c;while((c=getchar())!='\n'){if(c=='S') b[j++]=i;i++;}int max=0;if(j==0) max=i;//防止全部都是G的情况 if(b[1]-1>=max) //交换第一个S {if(b[1]-1 < n-j) max=b[1];else max=b[1]-1;} if(n-b[j-2]-2>=max) //交换最后一个S {if(n-b[j-2]-2 < n-j) max=n-b[j-2]-1;else max=n-b[j-2]-2;}for(int k=0;k<j-1;k++)if(b[k+1]-b[k]-1>=max) max=b[k+1]-b[k]-1;for(int k=1;k<j-1;k++)if(b[k+1]-b[k-1]-2>=max) {if(b[k+1]-b[k-1]-2 < n-j) max=b[k+1]-b[k-1]-1;else max=b[k+1]-b[k-1]-2;}if(n-j==0) cout<<"0";
//  else if(n-j==1) cout<<"1";else cout<<max;return 0;
}

分析

给定一串字符,每次都记录s的位置,两个相邻s的位置差-1就是一串连续的金牌数(至于为什么-1:5和7之间只有一个6,7-5=2)

举个例子: 第五个s 第六个s 第七个s... 如字符串:   S4GGGS5GGS6GGGS7GGS8S9

如果把S6换成金牌,那么新的连续g区间就是S6,S5之间的g数量+ S7,S6数量+1

这个时候要想一想了 如果是 SGGGSGGS这种情况 就不能+1了

总起来说就是,要比较S7,S6之间的g数量+ S6,S5之间的g数量是否等于全部的g数量(如果等于,说明别的地方没有g了他,也就只能拿自身的g来替换s了,那么就不能+1;其他情况就说明了其他地方还有g可以替换此处的s,理所当然的+1)

也就是

1:如果某个s前后的g的个数总和小于总共g的个数,说明可以从其他地方拿一个,这样的话就可以将s前后的两个g串合起来然后长度+1;

2:如果某个s前后的g的个数总和等于总共g的个数(比如ggggsggg)那么就是将一个串中拿出一个g替换s,这样长度就是两个g串长度的和

上一段代码就是这么做的,但是b数组是从0开始的,需要单独考虑第一个s和最后一个s。

因此想到了一种方法,让b数组从1开始,b[0]用来存放-1,就是假装第一个s之前还有一个s,这个s位置是-1;假装最后一个s之后还有一个s,这个s是n

还是蛮好理解的,比如GSGGSGG,那么加上这两个sGSGGSGGs,这样就可以一起放到for循环计算,不用单独拿出第一个s和最后一个s来特殊考虑了

代码如下

#include<bits\stdc++.h>
using namespace std;
int b[100010];
int main()
{int n,i=0,j=1;cin>>n;getchar();char c;while((c=getchar())!='\n'){if(c=='S') b[j++]=i;i++;}int num=n-j+1;//num表示G的个数 j-1表示s个数 int max=0;b[0]=-1;b[j]=n; if(n==num) max=i;//全部都是G的情况 for(int k=1;k<j;k++)if(b[k+1]-b[k-1]-2>=max) {if(b[k+1]-b[k-1]-2 < num) max=b[k+1]-b[k-1]-1;else max=b[k+1]-b[k-1]-2;}if(num==0) cout<<"0";else cout<<max;return 0;
}

Vova and Trophies _Codeforces 1082B相关推荐

  1. CF 1082B Vova and Trophies

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

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

  3. 【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

    题干: Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The ...

  4. CodeForces - 1082B Vova and Trophies

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

  5. Vova and Trophies

    题意翻译 题目描述 你现在有 nn 枚奖牌,每枚奖牌为金牌或银牌.这些奖牌现在按顺序排成一排.现在你可以调换任意一对奖牌的位置,求金牌最长连续段. 输入格式 第一行一个整数 nn (2\le n\le ...

  6. B. Vova and Trophies

    链接 [https://codeforces.com/contest/1082/problem/B] 题意 给你一个包含GS的字符串,只允许交换一次任意不同位置的字符,问最长的连续G串是多少 分析 很 ...

  7. Educational Codeforces Round 55 (Rated for Div. 2)

    A.Vasya and Book 三种情况讨论一下 1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 ...

  8. 店宝宝:海外版“拼多多” 低调的Vova能否创辉煌?

    如何定义拼多多? 据了解,拼多多官方给出的定义是:拼多多是一家基于人工智能大数据技术和移动互联网的新电商平台.其基础模式是利用移动互联网技术,通过大数据算法了解消费者的需求,然后为消费者寻找能够满足其 ...

  9. 干货分享,vova跨境电商平台引流及物流优势

    Vova跨境电商平台靠谱吗?Vova平台目前属于海蓝市场,而且之前vova平台出现的问题也正在完善当中.Vova平台千万不要做?许多卖家对vova平台争议很大,导致许多想入驻vova的小白只能驻足观望 ...

最新文章

  1. 17种深度强化学习算法用Pytorch实现(附链接)
  2. Tegra3 vSMP架构Android运行时CPU热插拔及高低功耗CPU切换
  3. mysql 散列存储_什么是数据库散列存储? - 蚂蚁吞大象的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  4. Azkaban的Web Server源码探究系列20:resolvebuildFlow
  5. java注解如何设置自增长_java如何自定义注解(一)
  6. mysql+encode+decode+错误_mysql decode encode 乱码问题
  7. 斯诺登:FBI需要苹果帮助才能解锁iPhone完全扯淡
  8. html关于拖放叙述错误,CIW页面设计与制作HTML附答案
  9. 行存储索引改换成列存储索引_索引策略–第2部分–内存优化表和列存储索引
  10. vue.js解决刷新404找不到页面问题
  11. #if defined和#if !defined的含义
  12. WSL自定义安装路径
  13. 读取SPRING XML配置文件中的hbm.xml 文件列表.
  14. vue+vuex+vur-router+ElementUI+axios +springboot打造响应式博客项目
  15. SSM框架客户管理系统
  16. SQL Server 2008 R2 完全卸载
  17. AI笔记: 计算机视觉之图像边缘检测: Robert算子, Sobel算子, Laplace算子, LoG算子, Canny算子
  18. 雷军:《我十年的程序员生涯》系列之三:阳光灿烂的日子
  19. IEC101 可变结构限定词、传送原因、ASDU公共地址和传送原因
  20. R语言函数-tolower

热门文章

  1. c语言两层循环如何跳到最外层,c语言如何跳出多层循环
  2. 有关飞行的42经典电影,不看会后悔哦!
  3. CC10000.CloudKubernetes——|CloudNative|
  4. CF140E New Year Garland (计数问题)
  5. WorkManager笔记: 三、加急工作
  6. ipad iphone开发_如何在iPhone或iPad上阅读PDF文件
  7. 80后十三种最深沉的寂寞
  8. 判断MOS管好坏的方法有两种
  9. 端口是什么东西?为什么老是被黑客利用
  10. python变量pi和pi被看作相同的变量_python分享pi的方法 两种用python分享p