链接

[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. centos7下安装指定版本mysql5.7.23

    现在mysql版本已经到MySQL 8.0(GA)稳定版本了,所以需求是想简单又快速在centos7下安装指定版本例如MySQL 5.7(GA)版本有下面这种方法 首先需要到mysql官网这里下载对应 ...

  2. 实验吧web解题记录

    自以为sql注入掌握的还是比较系统的,然而,做了这些题之后才发现,大千世界无奇不有,真是各种猥琐的思路...还是要多学习学习姿势跟上节奏 登录一下好吗?? http://ctf5.shiyanbar. ...

  3. January 17th, 2018 Week 03rd Wednesday

    Don't let go too soon, but don't hold on too long. 不要太快放手,也别紧握太久. It is inevitalbe to encounter with ...

  4. 超详细!Github团队协作教程(Gitkraken版)

    超详细!Github团队协作教程(Gitkraken版) 一.前期工作 1. 在 Github 上创建 organization step1. 登录Github网站,点击右上角头像,选择 " ...

  5. vue+webpack安装sass过程中遇到权限不够,直接删除node_modus文件夹重新安装,node_modus先取得管理员权限才能删

    vue vue-style-loader !css-loader错误 最近在学习vue框架,使用webpack打包vue项目,在执行npm run start的时候 出现如下错误: This depe ...

  6. Android AbsListView Abs前缀

    Android AbsListView Abs abstract:抽象

  7. js深度克隆对象

    js深度克隆对象 js深度克隆对象简单的记录一下,如下代码: var obj = { typeOf: function(obj) { const toString = Object.prototype ...

  8. Objective-C 基于Aspects

    JavaScriptCore没有禁,因为各种小程序都在用 网络下载文件没有禁

  9. 【Codeforces 1110D】Jongmah FST分析

    Codeforces 1110 D FST分析 dotorya.FizzyDavid.MofK.gamegame.matthew99.chokudai.eddy1021.DBradac.Happy_N ...

  10. Selenium:浏览器及鼠标、键盘事件

    参考文档:WebDriver官方文档,下载链接:http://download.csdn.net/detail/kwgkwg001/4004500 虫师:<selenium2自动化测试实战-基于 ...