Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)
Posted xwl3109377858
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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".
题意:题目意思就是给你一个只由 ‘R‘ ‘B‘ 和 ‘G‘ 三种字符组成的字符串,
问你最少要改变几个字符才能变成 "RGBRGBRGB..."的子串。
思路:这题跟 D2 题意一样,只不过数据简单,既然是 "easy version" ,那显然我们直接暴力就好了。
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
以上是关于Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #575 (Div. 3)
Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)
Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)
Codeforces Round #575 (Div. 3) RGB Substring (hard version) ( FFT)
Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
Codeforces Round #575 (Div. 3) (A. Three Piles of Candies)(数学)