Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3)

 

D1 - RGB Substring (easy version)

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.

Input

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 n characters 'R', 'G' and 'B'.

It is guaranteed that the sum of n 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 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 ...".

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".

In the second example, the substring is "BRG".

 

The meaning of problems: Title means is to give you a string of only 'R' 'B' and 'G' three kinds of characters,

Ask you at least want to change a few characters in order to become "RGBRGBRGB ..." substring.

Thinking: This is the same problem with D2 meaning of the questions, but the data is simple, since it is "easy version", it is clear we direct violence enough.

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 using namespace std;
11 #define ll long long 
12 const int inf=1e9+7;
13 const int mod=1e9+7;
14  
15 const int maxn=1e5+5;
16  
17 char nextt[100];
18  
19 int jisuan(string str)
20 {
21     int minn=inf,ans;
22     char now;
23     
24     ans=0;
25     now='R';//从'R'开始匹配 
26     for(int i=0;i<str.size();i++)
27     {
28         if(str[i]!=now)
29             ans++;
30         
31         now=nextt[now];
32     }
33     if(ans<minn)
34         minn=ans;//刷新最小值 
35         
36     ans=0;
37     now='G';//从'G'开始匹配 
38     for(int i=0;i<str.size();i++)
39     {
40         if(str[i]!=now)
41             ans++;
42         
43         now=nextt[now];
44     }
45     if(ans<minn)
46         minn=ans;//刷新最小值 
47          
48     ans=0;
49     now='B';//从'B'开始匹配 
50     for(int i=0;i<str.size();i++)
51     {
52         if(str[i]!=now)
53             ans++;
54         
55         now=nextt[now];
56     }
57     if(ans<minn)//刷新最小值 
58         minn=ans;
59     
60     return minn;
61 }
62  
63 int main()
64 {
65     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
66     
67     nextt['R']='G';
68     nextt['G']='B';
69     nextt['B']='R';
70     
71     int T;
72     cin>>T;
73     int n,k,ans;
74     string str,now;
75     while(T--)
76     {
77         cin>>n>>k;
78         cin>>str;
79         
80         int minn=inf;
81         
82         for(int i=0;i<n-k+1;i++)
83         {
84             now=str.substr(i,k);//从字符串中依次截取k长度的子串 
85         //    cout<<now<<endl;
86             
87             ans=jisuan(now); 
88             
89             if(ans<minn)
90                 minn=ans;
91             
92         }
93         
94         cout<<minn<<endl;
95     }
96     
97     return 0;
98 }

 

这里放一下标程1写法,讲道理还是我写的比较容易懂,,,

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<algorithm>
 9 #include<queue>
10 #include<unordered_map>
11 using namespace std;
12 #define ll long long 
13 const int mod=1e9+7;
14 const int inf=1e9+7;
15  
16 //const int maxn=
17  
18 int main()
19 {
20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
21     
22     const string t="RGB";
23     
24     int T;
25     cin>>T;
26     int n,k;
27     string str;
28     while(T--)
29     {
30         cin>>n>>k>>str;
31         
32         int minn=inf;
33         
34         for(int i=0;i<n-k+1;i++)
35         {
36             for(int now=0;now<3;now++)
37             {
38                 int cnt=0;
39                 for(int j=0;j<k;j++)
40                 {
41                     if(str[i+j]!=t[(j+now)%3])
42                         cnt++;
43                 }
44                 if(cnt<minn)
45                     minn=cnt;
46             }
47         }
48         
49         cout<<minn<<endl;
50         
51     }
52     
53     return 0;
54 }

 

Guess you like

Origin www.cnblogs.com/xwl3109377858/p/11271930.html