链接

[https://codeforces.com/contest/1082/problem/B]

题意

给你一个包含GS的字符串,只允许交换一次任意不同位置的字符,问最长的连续G串是多少

分析

很显然贪心找相邻的中间间隔一个S的两个连续G串

看了别人写的很巧妙,多用几个样例就知道其中的奥妙了

代码

#include<bits/stdc++.h>
using namespace std;
int main(){
int n; string s;
//freopen("in.txt","r",stdin);
while(cin>>n){
cin>>s;
int cntG=0,cntS=0,sumG=0,ans=0;
for(int i=0;i<n;i++){
if(s[i]=='G'){
cntG++; sumG++;
}
else{
cntS=cntG; cntG=0;//遇到scntG从新归零统计G连续的个数
}
ans=max(ans,cntG+cntS+1);//假设都可以交换带来收益
}
ans=min(ans,sumG);//这里很关键存在交换没有带来收益的
cout<<ans<<endl;
}
return 0;
}

B. Vova and Trophies的更多相关文章

  1. Codeforces 1082B Vova and Trophies(前缀+后缀)

    题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...

  2. Codeforces 1082B Vova and Trophies 模拟,水题,坑 B

    Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...

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

  4. Educational Codeforces Round 55:B. Vova and Trophies

    B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...

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

  6. CF1082B Vova and Trophies 题解

    CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...

  7. Vova and Trophies CodeForces - 1082B(思维题)

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

  8. 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将所有奖杯排成一排,你 ...

  9. B. Vova and Trophies 字符串预处理+思维+贪心

    题意:给出一个字符串 只有G和S  可以交换任意两个位置的字符一次 问 最长的G的长度是多少 思路:预处理字符串 把相同的G粘成一个G 记一下数量  字符串变为 GSSGSGGSGSSG 相邻有一个S ...

随机推荐

  1. Fedora 28 打印机配置 ( HP pro 1136M ,基于Windows 打印服务器使用 smb 协议)

    Fedora 28 本身是没有打印服务的.我们需要安装下列软件: System-Config-Printer Common Unix Printing System - CUPS hplip.x86_ ...

  2. ccf--20150303--节日

    本题思路:首先,计算a月1日是星期几,然后再通过b和c得出日期monday,最后判断monday是否合法. 题目与代码如下: 问题描述 试题编号: 201503-3 试题名称: 节日 时间限制: 1. ...

  3. ArcGIS栅格影像怎么从WGS84地理坐标转成Xian80投影坐标

    事情是这样的,我下载了一个WGS84坐标系的影像图,需要加载到Xian80投影坐标系下,所以需要对影像图进行坐标系的转换 1.因为涉及到两个参考椭球的问题,首先需要计算七参数,如何计算七参数,请参考我 ...

  4. jquery计算时间差(天、时、分、秒)并使用定时器实时获取

    类似网站抢购需求,会有个时间倒计时的展示(天.时.分.秒) 要拿到最终时间与当前时间对比,算出时间差并用定时器以秒的方式执行 实现代码: $(document).ready(function(){ r ...

  5. python六十三课——高阶函数之sorted

    演示sorted函数的使用,以及和sort的区别:我们将sorted和sort进行一番比较:相同点:它们都是来实现排序的操作(功能层面)不同点:列表中的sort函数,它执行完毕后会直接影响原本这个li ...

  6. php如何控制用户对图片的访问 PHP禁止图片盗链

    本文摘自网络仅供学习只用 本人根据教程总结了一下https://www.imooc.com/video/13412 主要是利用apache的htacess进行控制,,拿什么判断是不是通过本站点访问的呢 ...

  7. call()、delegatecall()

    call()当使用方法是:require(msg.sender.call.value(_weiToWithdraw)());则作用是将_weiToWithdraw个以太币发送给msg.sender地址 ...

  8. solidity数据位置-memory,storage和calldata

    有三种类型,memory,storage和calldata,一般只有外部函数的参数(不包括返回参数)被强制指定为calldata.这种数据位置是只读的,不会持久化到区块链 storage存储或memo ...

  9. mysql 创建表格 AUTO_INCREMENT

    CREATE TABLE `t_user` ( `USER_ID` int(11) NOT NULL AUTO_INCREMENT, `USER_NAME` char(30) NOT NULL, `U ...

  10. 理解 DocumentFragment

    理解 DocumentFragment 含义:创建文档片段,它继承了Node的所有方法,对DOM操作性能非常好.创建文档片段 如下方法: var frag = document.createDocum ...