D2. RGB Substring (hard version)||D1. RGB Substring (easy version)

D2. RGB Substring (hard version)    原题传送门

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The only difference between easy and hard versions is the size of the input.

You are given a string ss consisting of nn characters, each character is ‘R‘, ‘G‘ or ‘B‘.

You are also given an integer kk. Your task is to change the minimum number of characters in the initial string ss so that after the changes there will be a string of length kk that is a substring of ss, and is also a substring of the infinite string "RGBRGBRGB ...".

A string aa is a substring of string bb if there exists a positive integer ii such that a1=bia1=bi, a2=bi+1a2=bi+1, a3=bi+2a3=bi+2, ..., a|a|=bi+|a|−1a|a|=bi+|a|−1. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Then qq queries follow.

The first line of the query contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string ss and the length of the substring.

The second line of the query contains a string ss consisting of nn characters ‘R‘, ‘G‘ and ‘B‘.

It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each query print one integer — the minimum number of characters you need to change in the initial string ss so that after changing there will be a substring of length kk in ss that is also a substring of the infinite string "RGBRGBRGB ...".

Example

input

3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR

output

1
0
3

Note

In the first example, you can change the first character to ‘R‘ and obtain the substring "RG", or change the second character to ‘R‘ and obtain "BR", or change the third, fourth or fifth character to ‘B‘ and obtain "GB".

In the second example, the substring is "BRG".

题意:

有个“RGB”无限循环的长母串,q组样例,每组一个n长的字符串,让你通过更改n长串的某个字符构造一个长度为k的,既是母串的子串,也是n长串的子串。求满足要求的最少更改次数。

思路:

母串t:BGGGG(3种)
样串s:RBGRBGRBGRBGRBGRBG
贡献a:11011 (不同的话,在这一位的贡献就是1,这个也是3种)

然后因为的们要获得区间长度为K的最小贡献,所以求个a的前缀和
O(1)遍历每一个长度为K的子串,找出最小的那个就是ans

#include<iostream>
#include<cstring>
using namespace std;
const int M=2e5+10;
char s[M];
int a[3][M];
int T,n,k,ans;
int main() {
    cin>>T;
    while(T--) {
        a[0][0]=a[1][0]=a[2][0]=0;
        cin>>n>>k;
        cin>>s;
        ans=M;
        string t="RGB";
        for(int i=0; i<n; i++) {
            for(int j=0;j<=2;j++){
                if(s[i]!=t[(i+j)%3])a[j][i+1]=a[j][i]+1;
                else a[j][i+1]=a[j][i];
            }
        }//a从下标为1的位开始&前缀和
        for(int i=0; i+k<=n; i++) {
            for(int j=0;j<=2;j++){
                ans=min(ans,a[j][i+k]-a[j][i]);
            }
        } //遍历求ans
        cout<<ans<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/geraldg/p/11247810.html

时间: 2024-07-30 14:03:16

D2. RGB Substring (hard version)||D1. RGB Substring (easy version)的相关文章

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

RGB 与 (RGB转 YCbCr再转为 RGB)的图像

RGB 与 (RGB转 YCbCr再转为 RGB)的图像   不可逆,可以从 矩阵的逆运算看出来. 附上 matlab 代码: clc,clear; Source=imread('1.jpg');%读入原始RGB图像 figure(1); subplot(1,2,1); imshow(Source):title('original image');%显示图像 [r c d]=size(Source);%计算图像大小 %------计算红色分量并显示分解图------% R(:,:,1)=Sour

D1. Kirk and a Binary String (easy version)

D1. Kirk and a Binary String (easy version) 01串找最长不降子序列 给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致 从后往前暴力枚举,枚举每个一替换成0后是否改变了l到r位置的最长不降子序列长度 01串的最长不降子序列,可以通过线性dp求解 dp i表示以i结尾的最长不降子序列长度 dp[0]=dp[0]+s[i]=='0'; dp[1]=max(dp[0],dp[1])+s[i]=='1'; #include<bi

错误:Cannot mix incompatible Qt library (version 0x50601) with this library (version 0x50602) 已放弃

今天在编译完deepin最新的软件包安装器,运行的时候出现Cannot mix incompatible Qt library (version 0x50601) with this library (version 0x50602) 已放弃这个错误,搜索了一下,发现网上的解决方法寥寥无几,但最终还是找到了方法成功解决了. 错误:Cannot mix incompatible Qt library (version 0x50601) with this library (version 0x50

2016 省热身赛 Earthstone: Easy Version

Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Earthstone is a famous online card game created by Lizard Entertainment. It is a collectible card game that revolves around turn-based mat

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was capture

File system needs to be upgraded. You have version null and I want version 7

安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hbase依赖于hdfs, hdfs是有nameservice的. 解决方法: 1. 首先查看hbase的配置文件,hbase-site.xml, 找到hbase.rootdir的value值, 这个值里面的/hbase1 中存放了hbase的信息 <property>        <name&

[Genymotion] [Fatal] Cannot mix incompatible Qt library (version 0x40806) with this library (version

最近genymotion提示更新,我就更新了,Ubuntu系统下,发现更新后不能正常打开了-提示错误如下: [Genymotion] [Fatal] Cannot mix incompatible Qt library (version 0x40806) with this library (version 0x40804) 于是乎,上百度查找半天,说是要安装QT什么的,但是我旧版本明明是可以用的啊,然后装各种东西,但是还是无法打开,后面想想,去官网看看吧,没想到真的有用. 解决方法 When

Unity: Invalid serialized file version xxx Expected version: 5.3.4f1. Actual version: 5.3.5f1.

Unity发布安卓项目,如果直接使用Unity打包APK一切Ok,导出Google项目 使用Idea打包 一进去直接Crash. 报错: 1978-2010/? E/Unity﹕ Invalid serialized file version. File: "/data/app/eran.project.b-1/base.apk/assets/bin/Data/globalgamemanagers". Expected version: 5.3.4f1. Actual version: