题目链接: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)的更多相关文章

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

  2. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  3. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  4. Codeforces 1196D2 RGB Substring (Hard version) 题解

    题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...

  5. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

  6. Codeforces 1196D2. RGB Substring (hard version)

    传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...

  7. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

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

  9. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

随机推荐

  1. java_第一年_JavaWeb(9)

    JavaBean是一个遵循某种特定写法的Java类,有以下特点: 必需具有一个无参的构造函数 属性必需私有化 私有化的属性必需通过public类型的方法暴露给其它程序,其方法命名也有一定的规范 范例: ...

  2. luogu P3657 (NOIP2017) 跳房子(二分+DP+单调队列)

    题面 传送门 分析 显然答案有单调性,可以二分答案,设当前二分值为g,根据题意我们可以求出跳跃长度的范围[l,r] 考虑DP 子状态: dp[i]表示跳到第i个点时的最大和 状态转移方程 \(dp[i ...

  3. mysql处理重复数据仅保留一条记录

    目的:去除(或删除)一个表里面手机号重复的数据,但是需要保留其中一个记录,换句话说,表里面手机号不为空的数据,一个手机有且只有一条记录 表结构: CREATE TABLE `account` ( `i ...

  4. installing-sql-server-2012-error-prior-visual-studio-2010-instances-requiring 转摘

    there are two way: First : Inside your CD of SQL Server 2012 you can go to this path \redist\VisualS ...

  5. ECharts 图表导出

    Echarts图形是由Javascript亲自在前端网页上绘制的 1.用ECharts配置项手册中的toolbox.feature.saveAsImage toolbox: { show: true, ...

  6. NGUI的sprite的使用(九宫切图)

    一,图集的添加和背景图的选择 图集的选择Atlas和背景图的选择Sprite 二,type的sliced切背景图的使用 当选择Sprite后的edit时,我们就可以设置当前背景图的边距,也有切图的意思 ...

  7. mySql | Error: ER_DATA_TOO_LONG: Data too long for column 'base_info' at row 1

    问题描述:执行insert语句报以下错误 原因:数据库表,该字段在设计的时候长度过小,新插入的数据过长,会提示以上错误! 解决办法:修改表中该字段的长度限定.

  8. CHEVP算法(Canny/Hough Estimation of Vanishing Points)

    这个算法是汪悦在 Lane detection and tracking using B-spline中提出来的.他在这篇论文中主要用的是B-spline模型,这个模型的主要优点是鲁棒性好,可以针对不 ...

  9. easyui datagrid数据网格

    EasyUI是一组基于jQuery的UI插件集合,它的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.它的许多控件让我们不必写很复杂的javascript,从而极大地提高了开发效率. ...

  10. Error- Overloaded method value createDirectStream in error Spark Streaming打包报错

    直接上代码 StreamingExamples.setStreamingLogLevels() val Array(brokers, topics) = args // Create context ...