RGB Substring (easy version)
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 (1≤q≤20001≤q≤2000 ) — the number of queries. Then qq queries follow.
The first line of the query contains two integers nn and kk (1≤k≤n≤20001≤k≤n≤2000 ) — 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 20002000 (∑n≤2000∑n≤2000 ).
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".
这道题因为数据范围较小,完全可以将字符串每一位开始的k位长的序列与三种情况(分别以vis[1][2][3]开头的)都比较一下,应该叫前缀处理?
然后取最小值。
#include<iostream>
using namespace std;
char res[3]={'R','G','B'};
int main(){
int T;
cin>>T;
while(T--){
int n,k;
cin>>n>>k;
string s;
cin>>s;
int ans=0x3f3f3f3f;
for(int i=0;i<n-k+1;i++){
int num[4]={0};
for(int t=0;t<k;t++){
if(s[i+t]!=res[t%3]){
num[0]++;
}
if(s[i+t]!=res[(t+1)%3]){
num[1]++;
}
if(s[i+t]!=res[(t+2)%3]){
num[2]++;
}
}
ans=min(min(ans,num[0]),min(num[1],num[2]));
}
cout<<ans<<endl;
}
return 0;
}