-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRGB_Substring.cpp
More file actions
50 lines (39 loc) · 1.09 KB
/
RGB_Substring.cpp
File metadata and controls
50 lines (39 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define DEBUG
#define MAXN 1000000
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
int query;
cin >> query;
const string t = "RGB";
while (query--) {
int length, sub_length;
string s;
cin >> length >> sub_length >> s;
//printf("new query\n");
int ans = MAXN;
// for each t-string
for (int t_offset = 0; t_offset < 3; ++t_offset) {
// RGB GBR BRG
// base case
int count = 0;
for (int i = 0; i < sub_length; ++i) {
if (s[i] != t[(t_offset+i)%3])
count++;
}
ans = min(count, ans);
for (int i = 1; i <= length - sub_length; ++i) {
if (s[i-1]!=t[(t_offset+i-1)%3]) count--;
if (s[i+sub_length-1]!=t[(t_offset+i+sub_length-1)%3]) count++;
ans = min(count, ans);
}
}
cout << ans << endl;
}
return 0;
}