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”.
解题思路:
思路虽然很简单,但是自己想的时候是想了好久才想出来的,后来看了题解,题解也是一样的解法。首先考虑枚举把每个字符都修改为‘R‘ ’G‘ ’B’ ,开一个二维数组,记录更改的开销,如果是他本身那么开销为0,其余两个都为1。初始化完了之后,用一个长度为K的滑动窗口,右边收录一个字符,加上对应的开销,而左边剔除一个字符,减去对应的开销。同样是要枚举三种情况,每一次移动都结算一次最小值,最后得到的就是全局最优解。那么这个窗口在初始化的时候,首先需要收录K个字符,先记录下他们的开销,以及开始和结束位置,接下来遍历整个串就行了,复杂度O(3*n)。
AC代码:
#include <bits/stdc++.h>
const int N = 1e6+10;
using namespace std;
int a[N][3]; // 记录每个字符的开销
map<char,int> mp; // 把字符映射成数字,便于处理
int main()
{
int n,m;
mp['R'] = 0;mp['G'] = 1;mp['B'] = 2;
string s;
int t;
cin>>t;
while(t--)
{
cin>>n>>m>>s;
for(int i = 0 ; i < n ; i ++ ) // 初始化开销数组
a[i][mp[s[i]]] = 0;a[i][(mp[s[i]]+1)%3] = 1;a[i][(mp[s[i]]+2)%3] = 1;
int head1,head2,head3;
int tail1,tail2,tail3;
int sum1,sum2,sum3;
head1 = 0 ; head2 = 1 ; head3 = 2 ;
tail1 = 0 ; tail2 = 1 ; tail3 = 2 ;
sum1 = sum2 = sum3 = 0;
for(int i = 0 ; i < m ; i ++) // 初始化长度为K的窗口
{
sum1 += a[i][head1];
sum2 += a[i][head2];
sum3 += a[i][head3];
head1 = (head1+1)%3;
head2 = (head2+1)%3;
head3 = (head3+1)%3;
}
int ans = min(sum1,min(sum2,sum3));
for(int i = m ; i < n ; i ++) // 遍历数组,更新最小值
{
sum1 += a[i][head1];
sum2 += a[i][head2];
sum3 += a[i][head3];
sum1 -= a[i-m][tail1];
sum2 -= a[i-m][tail2];
sum3 -= a[i-m][tail3];
head1 = (head1+1)%3;
head2 = (head2+1)%3;
head3 = (head3+1)%3;
tail1 = (tail1+1)%3;
tail2 = (tail2+1)%3;
tail3 = (tail3+1)%3;
ans = min(min(ans,sum1),min(sum2,sum3));
}
cout << ans << endl;
}
return 0;
}