RGB Substring
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 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⋅10^5) — 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⋅10^5).
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".
题目大意:
给定q个查询,每个查询包含两个数字n,k和一个字符串s,求在s中得到一个长度为k的子串,同时这个子串也是一个infinite字符串"RGBRGBRGBRGB..."的子串,最少需要改动的字符数。
题解:
做法类似尺取法,首先初始化三个infinite字符串,"RGBRGB...","GBRGBR...","BRGBRG...",因为infinite字符串的循环节是3,因此让字符串s分别和这三个infinite字符串比较便可以得到改变任意子串的代价。
以长度为k从左到右依次处理,每次取四个字符相同位置的四个长度为k的子串计算代价,保留这个过程中的最小值便得到答案。由于这个过程是递推的,对于每组查询只需要将各个字符串扫一遍即可,因此复杂度是 O(n)。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 2e5 + 5;
char a[maxn];
char x[maxn], y[maxn], z[maxn];
int main()
{
int q;
int n,k;
int pos1, pos2, pos3;
pos1 = 0;
pos2 = 1;
pos3 = 2;
char sub[] = "RGB";
for(int i = 0;i < maxn; ++i)
{
x[i] = sub[pos1];
y[i] = sub[pos2];
z[i] = sub[pos3];
pos1 = (pos1 + 1) % 3;
pos2 = (pos2 + 1) % 3;
pos3 = (pos3 + 1) % 3;
}
scanf("%d", &q);
while(q--)
{
scanf("%d %d", &n, &k);
scanf("%s", a);
getchar();
int ans = maxn;
int ans1 = 0;
int ans2 = 0;
int ans3 = 0;
for(int i = 0;i < k; ++i)
{
if(a[i] != x[i])
{
ans1 += 1;
}
if(a[i] != y[i])
{
ans2 += 1;
}
if(a[i] != z[i])
{
ans3 += 1;
}
}
ans = min(ans, min(ans1, min(ans2, ans3)));
for(int i = k;i < n; ++i)
{
ans1 -= a[i - k] == x[i - k] ? 0 : 1;
ans2 -= a[i - k] == y[i - k] ? 0 : 1;
ans3 -= a[i - k] == z[i - k] ? 0 : 1;
if(a[i] != x[i])
{
ans1 += 1;
}
if(a[i] != y[i])
{
ans2 += 1;
}
if(a[i] != z[i])
{
ans3 += 1;
}
ans = min(ans, min(ans1, min(ans2, ans3)));
}
printf("%d\n", ans);
}
return 0;
}