题目链接:Vova and Trophies

题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换。求连续G的最长长度。

题解:维护pre和pr,nxt和nx。pre和nxt像连续子段和一样维护G的个数,pr和nx维护分别前面和后面各有多少个G。

遍历每个位置i,pre[i-1]+nxt[i+1],判断当前位置i能不能用多余的G来替换,pr和nx判断一下是否与pre和nxt相同即可,过程中更新答案。

 #include <cstdio>
#include <algorithm>
using namespace std; const int N=2e5+;
typedef long long ll; char s[N];
ll pr[N],nx[N];
ll pre[N],nxt[N]; int main(){
int n;
ll ans=;
scanf("%d%s",&n,s+);
for(int i=;i<=n;i++){
pr[i]=pr[i-];
if(s[i]=='G') pre[i]=pre[i-]+,pr[i]++;
else pre[i]=;
}
for(int i=n;i>=;i--){
nx[i]=nx[i+];
if(s[i]=='G') nxt[i]=nxt[i+]+,nx[i]++;
else nxt[i]=;
}
for(int i=;i<=n;i++){
ll sum=;
sum=pre[i-]+nxt[i+];
if(s[i]=='G') ans=max(ans,sum+);
else{
if(pr[i-]!=pre[i-]||nx[i+]!=nxt[i+]) sum++;
ans=max(ans,sum);
}
}
printf("%lld\n",ans);
return ;
}

Codeforces 1082B Vova and Trophies(前缀+后缀)的更多相关文章

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

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

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

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

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

  4. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀

                                                                            D. "Or" Game       ...

  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. POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame

    题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...

  8. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  9. 1280 前缀后缀集合(map)

    1280 前缀后缀集合 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个数组包含N个正整数,其中有些是重复的.一个前缀后缀集是满足 ...

随机推荐

  1. Kasaraju算法--强连通图遍历及其python实现

    在理解有向图和强连通分量前必须理解与其对应的两个概念,连通图(无向图)和连通分量. 连通图的定义是:如果一个图中的任何一个节点可以到达其他节点,那么它就是连通的. 例如以下图形: 这是最简单的一个连通 ...

  2. QT信号槽connect的第五个参数

    用过QT的小伙伴都知道连接信号槽的connect方法,但是这个方法有第五个参数,一般都是用的默认的 connect(th,SIGNAL(started()),tmpmyobject,SLOT(show ...

  3. mssql sqlserver text数据类型专题说明

    摘要: 下文分享text数据类型的简介及处理text数据类型所涉及的函数,如下所示: text 数据类型简介: mssql sqlserver 常用数据类型简介 mssql sqlserver tex ...

  4. centos7个人shell编写环境

    一.配置存放文件/root/wang 存放常用的文件/root/wang/shell 存放练习的shell文件/root/wang/succeed_shell 存放有用shell文件/root/bak ...

  5. python粗谈面向对象(一)

    1.面向过程编程vs函数式编程 面向过程编程 以计算对象的元素个数为例. str_1 = 'abcdefg' count = 0 for i in str_1: # 统计字符串元素个数 count + ...

  6. windows搭建golang环境

    由于墙的存在,很多网址无法下载,推荐https://studygolang.com/dl去下载. windows需要配置几个环境变量,我是下载的压缩文件,所以需要自己配置,通过安装程序安装的应该不需要 ...

  7. python 3.7 配置mysql数据库

    一. mysql驱动安装 1.mysqlclient(推荐使用)    2.pymysql 二.django操作数据库     1.django配置连接数据库         settings.py ...

  8. LeetCode算法题-Reverse Words in a String III(Java实现)

    这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...

  9. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  10. C. Maximal Intersection(STL)

    这道题,关键在于怎么求多个区间的交集,使用multiset就可以 分别将 r , l 存在不同的mutiset中. 然后,我们来看一下 是不是 交集的 l 是最大的, 交集的 r 是最小的 #incl ...