题目链接: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. [BZOJ2138]stone(Hall定理,线段树)

    Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆 包含Ai颗石子.每1分钟,Nan会在编号在\([L_i,R_i] ...

  2. mysql中列转行

    通过group_concat()函数来实现 select group_concat(name) from table group by type select group_concat(name se ...

  3. jquery实现全选,反选,取消的操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 常见前端HTML5面试题

    1.H5新标签新特性 新标签:header,nav,footer,aside,article,section,Canvas,audio,video 新特性:localStorag, sessionSt ...

  5. CLR 垃圾回收知识梳理

  6. 2018-2-13-C#-搜索算法

    title author date CreateTime categories C# 搜索算法 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 +0 ...

  7. CF960G Bandit Blues 第一类斯特林数+分治+FFT

    题目传送门 https://codeforces.com/contest/960/problem/G 题解 首先整个排列的最大值一定是 \(A\) 个前缀最大值的最后一个,也是 \(B\) 个后缀最大 ...

  8. 特斯拉通过GitHub发布了这些源代码,用意何在?

    特斯拉多年来一直在软件社区使用开源软件而不遵守许可证. 在迈向合规性的一步中,特斯拉现在正在发布其软件的一些部分,这对特斯拉黑客和安全研究人员来说非常有用. 特斯拉是一家软件强大的公司,它一直在使用大 ...

  9. BZOJ3331 BZOJ2013 压力

    考前挣扎 圆方树这么早就出现了嘛... 要求每个点必须被经过的次数 所以就是路径上的割点/端点++ 由于圆方树上所有非叶子圆点都是割点 所以就是树上差分就可以辣. 实现的时候出了一点小问题. 就是这里 ...

  10. BestCoder Round #92 (hdu 6015 6016)

    比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...