-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRGB_Substring_DP.cpp
More file actions
49 lines (40 loc) · 1.21 KB
/
RGB_Substring_DP.cpp
File metadata and controls
49 lines (40 loc) · 1.21 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
// DP VERSION
#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;
int ans = MAXN;
int dp[3][length];
// the i+1-th is denote the different number of character of the previous i character
// cause if use i-th will need to write the base case
dp[0][0] = dp[1][0] = dp[2][0] = 0;
for (int t_offset = 0; t_offset < 3; ++t_offset) {
// RGB GBR BRG
// construct dp array
for (int i = 1; i <= length; ++i) {
if (s[i-1]!=t[(t_offset+i-1)%3])
dp[t_offset][i] = dp[t_offset][i-1] + 1;
else
dp[t_offset][i] = dp[t_offset][i-1];
}
for (int i = sub_length; i <= length; ++i) {
ans = min(ans, dp[t_offset][i]-dp[t_offset][i-sub_length]);
}
}
cout << ans << endl;
}
return 0;
}