Codeforces 1082B Vova and Trophies(前缀+后缀)
题目链接: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(前缀+后缀)的更多相关文章
- Codeforces 1082B Vova and Trophies 模拟,水题,坑 B
Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...
- 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 ...
- Educational Codeforces Round 55:B. Vova and Trophies
B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀
D. "Or" Game ...
- 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 ...
- CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...
- POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame
题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...
- hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 1280 前缀后缀集合(map)
1280 前缀后缀集合 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个数组包含N个正整数,其中有些是重复的.一个前缀后缀集是满足 ...
随机推荐
- MVP模式, 开源库mosby的使用及代码分析
Android中的构架模式一直是一个很hot的topic, 近年来Architecture components推出之后, MVVM异军突起, 风头正在逐渐盖过之前的MVP. 其实我觉得MVP还是有好 ...
- windows 服务中托管asp.net core
在windows 服务中托管asp.net core SDK 2.1.300 官方示例 1.添加运行标识符 xml <PropertyGroup> <TargetFramework& ...
- nginx 防止盗链
1.测试盗链(www.html2.com 盗取 www.html5.com的图片) 2.防止盗链 符合盗链 —— 重写 说明:if ($invalid_referer) {,if的后面是有空格的,如果 ...
- SQL Server捕获发生The query processor ran out of internal resources and could not produce a query plan...错误的SQL语句
最近收到一SQL Server数据库服务器的告警邮件,告警内容具体如下所示: DATE/TIME: 10/23/2018 4:30:26 PM DESCRIPTION: The query proc ...
- CADisplayLink以及定时器的使用
第一种: 用CADisplayLink可以实现不停重绘. - (CADisplayLink *)link { if (!_link) { // 创建定时器,一秒钟调用rotation方法60次 _li ...
- mas录屏,带系统声音和麦克风声音
自带的QuickTime + Soundflower 可完美解决,同时录系统的声音和mic声音,也可以只录系统声音. 安装Soundflower 在应用程序 -> 实用工具,里面找到“音频 MI ...
- HBase 数据模型
在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的映射. HBase数据模型术语 Table(表格) 一个HBase ...
- HTML DOM 事件对象 ondragend 事件
学习网站:http://www.runoob.com/jsref/event-ondragend.html 定义和用法 ondragend 事件在用户完成元素或首选文本的拖动时触发. 拖放是 HTML ...
- 在虚拟机中,设置centos7静态ip
https://blog.csdn.net/qq_34182808/article/details/80065908
- 洛谷 P1091 合唱队形
\[传送门在这里呀\] 题目描述 \(N\)位同学站成一排,音乐老师要请其中的\((N-K)\)位同学出列,使得剩下的\(K\)位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次 ...