题目链接: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. Python开发爬虫之动态网页抓取篇:爬取博客评论数据——通过Selenium模拟浏览器抓取

    区别于上篇动态网页抓取,这里介绍另一种方法,即使用浏览器渲染引擎.直接用浏览器在显示网页时解析 HTML.应用 CSS 样式并执行 JavaScript 的语句. 这个方法在爬虫过程中会打开一个浏览器 ...

  2. 亿级流量场景下,大型缓存架构设计实现【1】---redis篇

    *****************开篇介绍**************** -------------------------------------------------------------- ...

  3. 原生Js交互之DSBridge

    文章链接:https://mp.weixin.qq.com/s/Iqd0dKM-ZW4UwkIgSTnvYg 在上一篇文章「android 记一次富文本加载之路」中 介绍了关于android加载富文本 ...

  4. js计算剩余分钟

    // 剩余时间提醒 function checkTime() { if (timeCompare()) { document.getElementById('distanceDeadline').in ...

  5. Java文件输入保存,统计某个字符串,统计所有字符串

    import java.io.*; import java.util.*; /** * Created by Admin on 2018/3/20. */ public class FileSaveT ...

  6. kylin简单优化cube

    优化Cube 层次结构 理论上,对于N维,你最终会得到2 ^ N维组合.但是对于某些维度组,不需要创建这么多组合.例如,如果您有三个维度:洲,国家,城市(在层次结构中,“更大”维度首先出现).在深入分 ...

  7. 【原】Java学习笔记024 - 包装类

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 之前对于基本数据类 ...

  8. c/c++ 重载运算符 函数调用运算符

    重载运算符 函数调用运算符 把一个类的对象a,当成函数来使用,比如a(),所以需要重载operator()方法.重载了函数调用运算符的类的对象,就是函数对象了. 还有什么是函数对象呢??? lambd ...

  9. Springboot项目配置druid数据库连接池,并监控统计功能

    pom.xml配置依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> & ...

  10. 日志学习系列(三)——NLog基础知识

    前边我们解释了log4net的学习,我们再介绍一下NLog 一.什么是NLog NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码.NLog是一个简单 ...