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

Codeforces Round #575 (Div. 3)

 

D2 - RGB Substring (hard 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 an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...".

A string a is a substring of string b if there exists a positive integer i such that a1=bi, a2=bi+1, a3=bi+2, ..., a|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 q independent queries.

Input

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

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

The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'.

It is guaranteed that the sum of n over all queries does not exceed 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 s so that after changing there will be a substring of length k in s 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".

 

Meaning of the questions: one question on D1 comments (easy version), 2333

Idea: spend a D1 violence wording, it is clear TLE, then how do we do to optimize the algorithm,

Previous wording can look violent, every time the interception of a string again to match three times, in fact, there is clearly a repeat match,

Then we can save in matching state match, then each match finished, update the answer, move back one position,

Plus a character state, minus the character in front of a matching state, probably the ruler emulated thought.

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<algorithm>
 9 #include<queue>
10 #include<unordered_map>
11 using namespace std;
12 #define ll long long 
13 const int mod=1e9+7;
14 const int inf=1e9+7;
15  
16 const int maxn=2e5+10;
17  
18 int res[maxn];
19  
20 int main()
21 {
22     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
23     
24     const string t="RGB";
25     
26     int T;
27     cin>>T;
28     
29     int n,k;
30     string str;
31     
32     while(T--)
33     {
34         cin>>n>>k>>str;
35         
36         int minn=inf;
37         
38         for(int flag=0;flag<3;flag++)//三个不同RGB开始顺序匹配 
39         {
40             
41             int cnt=0;
42             
43             for(int i=0;i<k;i++)
44             {
45                 if(str[i]!=t[(i+flag)%3])
46                     cnt++;
47             }
48             
49             if(cnt<minn)
50                 minn=cnt;//刷新最小值 
51             
52             for(int i=k;i<n;i++)
53             {
54                 if(str[i]!=t[(i+flag)%3])//处理最后一个字符 
55                     cnt++;
56                 if(str[i-k]!=t[(i-k+flag)%3])//处理最前面一个字符 
57                     cnt--;
58                 
59                 if(cnt<minn)
60                     minn=cnt;//刷新最小值 
61             }
62             
63         }
64         
65         cout<<minn<<endl;
66     }
67     
68     return 0;
69 }

 

Guess you like

Origin www.cnblogs.com/xwl3109377858/p/11271989.html