题目链接: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. tmux多终端工具

    在Linux服务器上没有办法像在桌面系统一样开多个终端,所以有时后进行一些操作不是太方便,所以可以使用tmux工具,创建多个终端. 这里仅仅是简单的介绍一下如何创建多个终端和进行多个终端之间切换,tm ...

  2. [已解决]报错: Error response from daemon: conflict

    报错内容: Error response from daemon: conflict: unable to delete f5b6ef70d79b (must be forced) - image i ...

  3. 提交代码到github

    1. 下载git 点击download下载即可.下载地址:https://gitforwindows.org/ 2. 注册github github地址:https://github.com/ 一定要 ...

  4. LTP安装方法

    git clone https://github.com/linux-test-project/ltp.git apt-get install automake make autotools ./co ...

  5. 32.Group Anagrams(相同元素的不同组合)

    Level:   Medium 题目描述: Given an array of strings, group anagrams together. Example: Input: ["eat ...

  6. MVC路由学习:自定义路由参数(用户看不到参数名),重新定义路由规则

    MVC路由:由于路由global中注册了,在程序第一次运行时,在MVC会自动生成路由,类似于字典的格式缓存下来,但路由生成的规则又是怎样的呢? 路由生成规则是: 1>更具你定义的的顺序查找路由规 ...

  7. MySQL的数据类型:文本、数字、日期/时间

    在MySQL中,有三种主要的类型:文本.数字和日期/时间类型. 文本类型(text):数据类型                                 描述 CHAR(size) 保存固定长度 ...

  8. 解决MySQL在导入大文件时候,出现MySQL Server has gone away的问题

    编辑/etc/my.cnf文件,在[myslqd]节点,添加 max_allowed_packet = 64M 随后重启MySQL即可.

  9. ORA-01555 快照过旧

    用户user1对表进行了更新操作,用户user2在user1还没有进行提交前读表中数据,而且是大批量的读取(打个比方:耗时3分钟)而在这3分钟内user1进行了提交操作,那又会产生什么影响呢?这个时候 ...

  10. Sass-插值#{}

    使用 CSS 预处理器语言的一个主要原因是想使用 Sass 获得一个更好的结构体系.比如说你想写更干净的.高效的和面向对象的 CSS.Sass 中的插值(Interpolation)就是重要的一部分. ...