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

题干:

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。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441109.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java mac 转换 整形_JAVA的整型与字符串相互转换

1如何将字串 String 转换成整数 int?A. 有两个方法:1). int i Integer.parseInt([String]); 或i Integer.parseInt([String],[int radix]);2). int i Integer.valueOf(my_str).intValue();注: 字串转成 Double, Float, Long 的方法大同小异.2 如何将整数 int 转换成字串 St…

【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)

题干&#xff1a; Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. A palindrome is a string that reads the same backward as forward, for e…

java迷宫生成算法_DFS算法迷宫生成器

我正在尝试使用DFS算法在ASCII中创建迷宫(&#xff03;表示墙和自由空间)&#xff0c;其左上角开始&#xff0c;右下角出口 . 问题是迷宫开始创建&#xff0c;然后它被阻止&#xff0c;因为它的所有邻居都已被访问过 .我从左上角开始&#xff0c;将单元格标记为已访问并放置一个…

【牛客 - 272A】Phrase String(构造,水题)

题干&#xff1a; 给出v, k&#xff0c;请你找到最小的正整数n&#xff0c;满足&#xff1a; n的二进制表示下存在一个长度为v的回文串&#xff0c;该回文串首尾都是1且n的二进制表示中至少有k个1。保证v,k均为偶数&#xff01; 由于n可能很大&#xff0c;你只需要输出对取模的…

eclipse 跑maven命令_maven编写命令行执行mvn package没问题,eclipse执行报错

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼报错是这样的“[ERROR] Unknown lifecycle phase "Systemout3". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sou…

【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

题干&#xff1a; Mike has always been thinking about the harshness of social inequality. Hes so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A  [a1, a2, ..., an…

erlang odbc mysql参数_Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

个人在安装配置时遇到一些麻烦&#xff0c;特此记录如下&#xff1a; 环境 数据库服务器操作系统&#xff1a;Windows 2003 数据库&#xff1a;Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 erlang运行的服务器操作系统&#xff1a;Redhat 5.3 erlang:Erlang R1…

【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)

题干&#xff1a; Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 41944 Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any se…

halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...

如题&#xff0c;这个问题本人已经纠结了快三个工作日了。本人不同WinFrom程序一起动就会开启10个线程&#xff0c;并发对10张图片进行算法处理&#xff0c;问题是只要程序一起动就会报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”异常。本人试过将8个线程停掉…

【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干&#xff1a; Constructing Roads In JGShinings Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496 Problem Description JGShinings kingdom consists of 2n…

java list详解_Java 中 list 用法案例详解

LIST是个容器接口可以理解为动态数组&#xff0c;传统数组必须定义好数组的个数才可以使用&#xff0c;而容器对象无须定义好数组下标总数&#xff0c;用add()方法即可添加新的成员对象&#xff0c;他可以添加的仅仅只能为对象&#xff0c;不能添加基本数据类型&#xff0c;容器…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐的组合数+(取模,数学,思维)

题干&#xff1a; 小乐乐得知一周有7天之后就对7产生了兴趣。 小乐乐得到了两堆数字数字时连续的。 第一堆包含[1,n]n个数字&#xff0c;第二堆包含[1,m]m个数字。 小乐乐想要从两堆中各挑选出一个整数x,y&#xff0c;使得x,y的和为7的倍数。 请问小乐乐有多少种组合的方式…

java 写文件 属性吗_使用JAVA读写Properties属性文件

自己定义一个属性文件&#xff1a;例如prop.propertiesbaseFilePathD\:/kuanter/resourcetesxabcd我们要做的第一步就是要将文件读取到Properties类对象中&#xff0c;由于load有一个参数是InputStream&#xff0c;所以我们可以用 InputStream的子类FileInputStream将属性文件读…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】 小乐乐算数字(水题,快速幂,lowbit)

题干&#xff1a; 小乐乐最喜欢玩数字了。 小乐乐最近迷上了2这个整数&#xff0c;他觉得2的幂是一种非常可爱的数字。 小乐乐想知道整数x的最大的 2的幂 &#xff08;2^y&#xff09;的因子。 y为整数。 输入描述: 输入整数x。(1<x<1e18) 输出描述: 输出整数x的最…

java date 相差_java 比较时间相差多少分钟

/** * 返回二个时间相差的分分钟数,如果一个为空&#xff0c;返回为0&#xff1b; * param startDate&#xff0c;比如08&#xff1a;09 * param endDate&#xff0c;如18&#xff1a;09 * return */ public static int getMinutesDiff(String startDate,String endDate){ int …

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)

题干&#xff1a; 小乐乐是一个比较喜欢吃糖豆的小孩子&#xff0c;小乐乐的哥哥大乐乐也同样爱吃糖豆。 作为一个小孩子&#xff0c;他们永远觉得谁吃掉了最后一个糖豆&#xff0c;谁吃的糖豆最多。 为了公平起见小乐乐与大乐乐商量吃糖豆的规则如下&#xff1a; 1. …

html5 sse java_html5----sse实现服务端推送数据给前端

案例基于thinkPHP框架&#xff1a;服务端方法&#xff1a;public function ssefun(){ob_implicit_flush();header(Content-Type: text/event-stream);header(Cache-Control: no-cache);$itime();echo retry:1000.PHP_EOL;//每秒执行一次echo "data: The server time is: {…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)

题干&#xff1a; 小乐乐的作业本是2n*2n的方格本。 某天小乐乐的童鞋&#xff0c;想要考验一下小乐乐。 他将小乐乐的一张方格纸中的某个格子(x,y)涂成黑色&#xff0c; 小乐乐能否在将4*4的方格本沿着方格边缘且切割线与黑色方格不存在公共交点的情况下将方格本切割成两…

*【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐打游戏(bfs,双元bfs,思维)

题干&#xff1a; 小乐乐觉得学习太简单了&#xff0c;剩下那么多的时间好无聊&#xff0c;于是便想打游戏。 最近新出了一个特别火的游戏&#xff0c;叫吃猪&#xff0c;小乐乐准备玩一玩。 吃猪游戏很简单&#xff0c;给定一个地图&#xff0c;大小为n*m&…

java map 结构体_java之mapstruct的应用

一、MapStruct是一个代码生成器&#xff0c;简化了不同的Java Bean之间映射的处理&#xff0c;所以映射指的就是从一个实体变化成一个实体。例如我们在实际开发中&#xff0c;DAO层的实体和一些数据传输对象(DTO)&#xff0c;大部分属性都是相同的&#xff0c;只有少部分的不同…