链接

[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. Linux 小知识翻译 - 目录 (完结)

    偶尔在网上看到的这篇文章,总结了linux中一些天天在说的概念.可能是因为天天说的关系, 一直以为自己知道这些概念.其实对有些概念也只是似是而非的了解而已. 为此,决定将所有的小知识点全翻译下来,给自 ...

  2. Reachability

    一.Reachability中介绍了取得/检测网络状态的方法. 二.使用 1.添加源文件:Reachability.h和Reachability.m 2.添加framework———SystemCon ...

  3. 怎样将本地web项目部署到腾讯云服务器上?

    怎样将本地web项目部署到腾讯云服务器上? 1.本地计算机的工作: (1).用eclipse新建一个web项目,然后在webcontent下新建一个index.html,然后在本地部署到Tomcat服 ...

  4. 用Python读写Excel文件的方式比较

    虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...

  5. Python数据分析实例操作

    import pandas as pd #导入pandas import matplotlib.pyplot as plt #导入matplotlib from pylab import * mpl. ...

  6. UVA1599-Ideal Path(BFS进阶)

    Problem UVA1599-Ideal Path Time Limit: 3000 mSec Problem Description New labyrinth attraction is ope ...

  7. ubuntu 16.04 SS安装及配置

    安装SS客户端 安装pip3 一般情况下,pip3安装的版本比pip安装的新,pip安装的版本比apt安装的新,这里选择最新版本. sudo apt install python3-pip 安装SS ...

  8. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  9. PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  10. Android6.0权限大全和权限分类

    本文转载至: https://blog.csdn.net/qq_26440221/article/details/53097868 自从出了Android6.0权限管理之后,再也不能像以前那样粘贴复制 ...