time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
The first line of the input contains one integer q (1≤q≤2000) — the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1≤k≤n≤2000) — 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 2000 (∑n≤2000).
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 …”.
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
1
0
3
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”.
您将得到一个由n个字符组成的字符串s,每个字符都是“r”、“g”或“b”。
还为您提供了一个整数k。您的任务是更改初始字符串s中的最小字符数,以便更改后将有一个长度为k的字符串,该字符串是s的子字符串,也是无限字符串“rgbrgbrgbb…”的子字符串。
如果存在正整数i(a1=bi,a2=bi+1,a3=bi+2,…,a a=bi+a−1),则字符串a是字符串b的子字符串。例如,字符串“gbrg”、“b”、“br”是无限字符串“rgbrgbrgb…”的子字符串,而“gr”、“rgr”和“ggg”则不是。
要让原字符串中出现长度为 K 的“rgbrgbrgbb…”的子字符串,而这个字符串又是一个循环字符串,它又可以看成 “rgb” 、“gbr”或“brg” 循环形成的,所以要使原字符串中含有长度为 k 的字符串 ,只需要从第一个字符遍历使得它们符合这三种子串的排布,然后找到改动最小的哪一种
#include
using namespace std;
char c[3]={'R','G','B'};
int main(){
int q;
cin>>q;
while(q--){
int n,k;
cin>>n>>k;
int ss=1<<30;
string s;
cin>>s;
for(int i=0;i