Problem Description
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=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 qq independent queries.
Input
The first line of the input contains one integer q (1≤q≤2000) — the number of queries. Then qq queries follow.
The first line of the query contains two integers nn and k (1≤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 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 ...".
Examples
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRROutput
1
0
3
题意:q 组询问,每组给出一个长度为 n 的字符串与一个 "RGBRGBRGB..." 循环子串的长度 k,现在要所给的长度为 n 的子串中选择一个长度为 k 的子串,对其进行任意次代价为 1 的更改,即可将任意一个字符改为其他字符,使得这个子串与 "RGBRGBRGB..." 循环子串所匹配,求最小代价
思路:
由于数据范围不大,可以考虑直接进行暴力
"RGBRGBRGB..." 循环子串固定只有三种情况,即分别以:R、G、B 开头,因此对于长度为 n 的字符串,从第 1 个字符开始,枚举到第 n-k 个字符,对于所枚举的每个字符,从其开始再进行长度为 k 的枚举,三种固定情况的 "RGBRGBRGB..." 循环子串进行比较,记录最小价值,最后取这三种子串最小价值的最小价值即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
char str[N];
char t[3][N];
int minn[N];
int main(){
int q;
scanf("%d",&q);
while(q--){
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",str);
for(int i=0;i<k;i++){
if(i%3==0){
t[0][i]='R';
t[1][i]='G';
t[2][i]='B';
}
else if(i%3==1){
t[0][i]='G';
t[1][i]='B';
t[2][i]='R';
}
else if(i%3==2){
t[0][i]='B';
t[1][i]='R';
t[2][i]='G';
}
}
memset(minn,INF,sizeof(minn));
for(int i=0;i<n-k+1;i++){
int temp0=0,temp1=0,temp2=0;
int cnt0=0,cnt1=0,cnt2=0;
for(int j=i;j<i+k;j++){
if(str[j]!=t[0][cnt0++])
temp0++;
if(str[j]!=t[1][cnt1++])
temp1++;
if(str[j]!=t[2][cnt2++])
temp2++;
minn[i]=min(temp0,min(temp1,temp2));
}
}
int res=INF;
for(int i=0;i<n-k+1;i++)
res=min(res,minn[i]);
printf("%d\n",res);
}
return 0;
}