链接

[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. 常用DOS命令之通俗易懂篇

    目录 常用DOS命令之通俗易懂篇 Arp 命令 Assoc 关联 At 计划服务 Attrib 属性 Cd=chdir 目录 Cipher Cls 清屏 Color 颜色 Comp 比较 Compac ...

  2. 启动OpenVPN失败

    启动OpenVPN失败 文:铁乐与猫 [root@yunwei_OpenVPN openvpn]# systemctl status openvpn ● openvpn.service - LSB: ...

  3. January 15th, 2018 Week 03rd Monday

    We got things to do. Places to go. People to see. Futures to make. 我们有很多事情要做,有很多地方要去,有很多人要见,有很多美好的未来 ...

  4. golang的json序列化问题

    首先看一段代码: package main import ( "encoding/json" "fmt" ) type Result struct { //st ...

  5. svn 更新

    dev更新流程: 1.打开软件,文件—>打开,弹出右边对话框链接dev地址      2.在窗口输入cd /var/www/user 回车 3.输入svn up 则更新dev代码完成 本地提交到 ...

  6. 解决HTTP status code is not handled or not allowed

    /Books/>: HTTP status code is not handled or not allowed 2017-11-04 17:21:38 [scrapy.spidermiddle ...

  7. BZOJ1041:[HAOI2008]圆上的整点(数论)

    Description 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. Input 只有一个正整数n,n<=2000 000 000 Output 整点个数 Samp ...

  8. Mysql中的锁机制

    原文:http://blog.csdn.net/soonfly/article/details/70238902 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的 计算资源(如 ...

  9. Arduino IDE for ESP8266教程(0)配置IDE

    淘宝链接 https://detail.tmall.com/item.htm?id=540067174120&spm=a1z09.2.0.0.6f7c6509ujAvQs&_u=71q ...

  10. Python基础(4)列表、元组、字符串、字典、集合、文件操作

    列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 详见:http://www.cnblogs.com/alex3714/articles/5717620.html 1.列表和元 ...