길이 N의 string을 길이 K의 substring으로 나눈다
시작 문자가 R, G, B일 때, 세 가지 경우로 나눈다
R - RGBRGBRGB...
G - GBRGBRGBR...
B - BRGBRGBRG...
- 제목
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.
- 입력
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 nn characters 'R', 'G' and 'B'.
It is guaranteed that the sum of nn 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 ...".
예제 입력 1 | 3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRR |
예제 출력 1 | 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".
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int test_case, size, divide, change_min, change_R, change_G, change_B, temp;
string str;
cin >> test_case;
for (int n = 0; n < test_case; n++) {
change_min = 2000;
cin >> size >> divide;
cin.ignore();
cin >> str;
for (int m = 0; m < size - (divide - 1); m++) {
change_R = change_G = change_B = 0;
for (int i = m; i < m + divide; i++) {
if ((i - m) % 3 == 0) {
switch (str[i]) {
case 'R': {
change_G++;
change_B++;
break;
}
case 'G': {
change_R++;
change_B++;
break;
}
case 'B': {
change_G++;
change_R++;
break;
}
}
}
else if ((i - m) % 3 == 1) {
switch (str[i]) {
case 'G': {
change_G++;
change_B++;
break;
}
case 'B': {
change_R++;
change_B++;
break;
}
case 'R': {
change_G++;
change_R++;
break;
}
}
}
else if ((i - m) % 3 == 2) {
switch (str[i]) {
case 'B': {
change_G++;
change_B++;
break;
}
case 'R': {
change_R++;
change_B++;
break;
}
case 'G': {
change_G++;
change_R++;
break;
}
}
}
}
temp = min(change_R, min(change_B, change_G));
if (change_min > temp)
change_min = temp;
}
cout << change_min << endl;
}
return 0;
}