[题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
题目链接:https://codeforces.com/problemset/problem/1196/D2
题意:
q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”、“G” 或 “B”。
求出更改初始字符串 s 中的最小字符数,以便更改后将有一个长度为 k 的字符串,该字符串是 s 的子字符串,也是无限字符串 “RGBRGBRGB…” 的子字符串
思路:
在无限字符串中有三种子串:“RGB...”,“GBR...”,“BRG...”
在这三种不同情况下,将所求字符串与原字符串比较
若不同,则贡献为 1,否则为 0
然后计算三种情况下的前缀和,枚举区间最小值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int sum[][]; int main()
{
int t,n,m;
string p="RGB",s;
cin>>t;
while(t--){
int ans=;
cin>>n>>m;
cin>>s;
for(int j=;j<;j++){
for(int i=;i<=n;i++){
if(s[i-]!=p[(i+j)%])
sum[i][j]=sum[i-][j]+;
else
sum[i][j]=sum[i-][j];
}
}
for(int i=;i<;i++)
for(int j=m;j<=n;j++)
ans=min(sum[j][i]-sum[j-m][i],ans);
cout<<ans<<endl;
}
return ;
}
[题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)的更多相关文章
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- Codeforces 1196D2 RGB Substring (Hard version) 题解
题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)
传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...
- Codeforces 1196D2. RGB Substring (hard version)
传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- CF #579 (Div. 3) D1.Remove the Substring (easy version)
D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
随机推荐
- 国内常用Linux镜像站点
网易镜像站点 http://mirrors.163.com/ 搜狐镜像站点 http://mirrors.sohu.com/ 阿里云镜像站点 http://mirrors.aliyun.com/ 北京 ...
- shell脚本从入门到精通(初级)之入门篇
写在开头 本文是阅读<Linux命令行与shell脚本编程大全>时的一些笔记,主要是shell脚本的一些基本语法, 还有很多细节和高级内容没有写到. 笔者也是shell script菜鸟, ...
- java绘制带姓的圆
public class ImageGenerator { private static final Color[] colors = new Color[] { new Color(129, 198 ...
- React(4) --引入图片及循环数据
引入图片的方法 1.引入本地图片 方法1: import logo from '../assets/images/1.jpg'; <img src={logo} /> 方法2: <i ...
- elasticsearch相关聚合查询示例
索引(index):logstash-nginx-*,type:nginx_access 请求路径: 1.按照某个字段进行分组统计访问量 { "query": { "bo ...
- mongodb 多表关联处理 : 内嵌以及连接(手动引用、DBref) 、aggregate中$lookup
MongoDB与关系型数据库的建模还是有许多不同,因为MongoDB支持内嵌对象和数组类型.MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).那么何时Embed何时Li ...
- ElasticSearch1.7 java api
package cn.xdf.wlyy.solr.utils; import java.util.ArrayList;import java.util.HashMap;import java.util ...
- canvas 图片反色
代码实例: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...
- BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2213 https://loj.ac/problem/2161 题解 做一道简单题来放松一下. ...
- css 文字对齐
// html <div>姓名</div> <div>手机号码</div> <div>账号</div> <div>密 ...