[题解]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中出现的最右的位置 ...
随机推荐
- Excel 技巧
<!-- Excel跳转到指定行指定列 --> =HYPERLINK("#"&ADDRESS(要跳转到的行数,要跳转到的列数),"跳转")
- indexOf与includes的区别
indexOf与includes的区别:https://blog.csdn.net/gtLBTNq9mr3/article/details/78700118 includes和indexOf相比较:相 ...
- call,apply,bind的用法和细节差异
call,apply,bind的用法 call,apply和bind都用来改变js中this对象的指向 var dog = { name:'dog', speak: function(value){ ...
- android项目的目录结构讲解
参考书籍:<第一行代码Android> 一:android项目的目录结构讲解 1..gradle和.idea 这两个目录下放置的都是Android Studio自动生成的一 ...
- Codeforces 1012B Chemical table (思维+二分图)
<题目链接> 题目大意:给定一个n*m的矩阵网格,向其中加点,对于一个组成矩形的四个点中如果有三个点中有元素,那么第四个点中会自动产生新的元素.问你最少再加多少个点能够填满这个网格.解题分 ...
- 表单验证之在a标签跳转之前执行其他操作(DOM与$两种实现方式)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JVM(10)之 年老代收集器
开发十年,就只剩下这套架构体系了! >>> 在上一篇博文我们介绍了JAVA新生代收集器,本篇博文我们要讲的就是关于老年代的一些收集器.老年代存活的一般是大对象以及生命很顽强的对象 ...
- 二进制部署k8s
一.二进制部署 k8s集群 1)参考文章 博客: https://blog.qikqiak.com 文章: https://www.qikqiak.com/post/manual-install-hi ...
- ORACLE USER视图
select * from USER_ALL_TABLES -- 包含对用户可用的表的描述. select * from USER_ARGUMENTS --列出对用户可存取的对象中的参数 ...
- 13-H.264编码解码器的无线应用:1080P60 3D无线影音传输器
H.264编码解码器的无线应用:1080P60 3D无线影音传输器 一.应用领域 家庭媒体娱乐中心 新闻现场采访 无线3D投影机 高清视频会议终端无线延长器 教学,医疗示教 考古,高档商业区域,监狱等 ...