题意翻译

题目描述

你现在有 nn 枚奖牌,每枚奖牌为金牌或银牌。这些奖牌现在按顺序排成一排。现在你可以调换任意一对奖牌的位置,求金牌最长连续段。

输入格式

第一行一个整数 nn (2\le n\le 10^5)(2≤n≤105) .

第二行一行字符串,表示你的奖牌。 ( \texttt{G}G 表示金牌,\texttt{S}S 表示银牌)

输出格式

输出一个整数表示答案。

题目描述

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.

输入输出格式

输入格式:

The first line contains one integer nn ( 2 \le n \le 10^52≤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.

输出格式:

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

输入输出样例

输入样例#1: 复制

10
GGGSGGGSGG

输出样例#1: 复制

7

输入样例#2: 复制

4
GGGG

输出样例#2: 复制

4

输入样例#3: 复制

3
SSS

输出样例#3: 复制

0

说明

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 .

思路:记录字符串中每个s前面有多少个g,如果没有s,就只统计g的个数,然后保留到二维数组里,剩下的就是分情况讨论,1:如果某个s前后的g的个数总和小于总共g的个数,说明可以从其他地方拿一个,这样的话就可以将s前后的两个g串合起来然后长度+1;2:如果某个s前后的g的个数总和等于总共g的个数(比如ggggsggg)那么就是将一个串中拿出一个g替换s,这样长度就是两个g串长度的和;3,这个就比较简单了,说明没有s,只有g,直接打印

#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long int LL;
const int inf = 0x7f7f7f7f;
const int N=2e5+5;
int num[N];
int main()
{int n;vector<int>ve;int cnt=0;cin>>n;string str;cin>>str;int sumg=0;int count=0;for(int i=0;i<n;i++){if(str[i]=='S'){//遇到s,就将之前的g的个数保存的数组里ve.push_back(count);count=0;}else{count++;//用来统计每个s前面g的个数}}for(int i=0;i<n;i++){//记录g的总共个数if(str[i]=='G')sumg++;}if(str[n-1]=='G'){//特判一下最后一个不是Sve.push_back(count);}int ans=0;if(ve.size()==1){//cout<<ve[0]<<endl;return 0;}else{for(int i=0;i<ve.size()-1;i++){if(ve[i]+ve[i+1]<sumg)//第一种情况ans=max(ans,ve[i]+ve[i+1]+1);else{ans=max(ans,ve[i]+ve[i+1]);//第二种情况}}
}
cout<<ans<<endl;return 0;}

Vova and Trophies相关推荐

  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. CF 1082B Vova and Trophies

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

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

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

  4. Vova and Trophies _Codeforces 1082B

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

  5. B. Vova and Trophies

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

  6. CodeForces - 1082B Vova and Trophies

    传送 题意:一个只包含G和S的字符串,一次交换能使连续的G最长为多少. 思路:想要使只包含G的子串最长,显然贪心地将两个子串中间的S与一个不在子串的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. 《TCP/IP图解》读书笔记
  2. elasticsearch配置小记
  3. [置顶] C# 邮件发送方法【NetMail方式】
  4. SAP UI5 GM6 require sap.ui.core.Core
  5. linux查看逻辑卷命令,Linux命令--逻辑卷管理
  6. Go程序开发---Go环境配置:CentOS6.5+Go1.8标准包安装
  7. 苦逼的程序员怎么发展
  8. java Set无序集合 HashSet 的对象元素唯一性
  9. LY.JAVA面向对象编程.形式参数和返回值
  10. java冒泡怎么写_java 冒泡 又一种写法
  11. javascript 方法总结(Array篇)
  12. scv文件单元格内存在换行符
  13. Android pad适配札记
  14. Linux下 python调用讯飞离线语音合成(tts)
  15. Spring Boot3.0正式发布及新特性解读
  16. 数据库应用——DQL查询数据(连表查询 子查询 详细案例)
  17. 你的PC机或者终端,是怎么上网的?怎么连接到网络的?无线网怎么接入Internet,如何访问网络资源?
  18. 《程序员》2012年12期精彩内容:2012这一年
  19. Linux 安装gcc编译器
  20. activiti 文档_那些可多人协作编辑的在线文档工具

热门文章

  1. html—登录及注册页面设计
  2. 吸收金十字能量的气功
  3. opencv图片合成视频提高视频清晰度参数
  4. 中基鸿业家庭如何进行理财规划
  5. 【数论算法】最大公约数 最小公倍数
  6. 专访魅族Flyme杜鹏:立足服务开发者全面提升Flyme平台运维及安全
  7. VLC媒体播放器介绍
  8. 傻瓜式免费自助建站工具,菜鸟建站理想工具
  9. ssm+java计算机毕业设计职业高中智慧教学系统5vuz6(程序+lw+源码+远程部署)
  10. 如何用python处理excel做直方图_python可视化(5)——直方图-excel直方图