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

Posted geraldg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (1q21051≤q≤2⋅105) — the number of queries. Then qq queries follow.

The first line of the query contains two integers nn and kk (1kn21051≤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 21052⋅105 (n2105∑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;

 

 

 

  

以上是关于D2. RGB Substring (hard version)||D1. RGB Substring (easy version)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

Codeforces 1203D2 Remove the Substring (hard version)

题解 CF1203D2 Remove the Substring (hard version)

Codeforces Round #734 (Div. 3)-D2. Domino (hard version)-题解

Codeforces Round #734 (Div. 3)-D2. Domino (hard version)-题解