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 }

原文地址:https://www.cnblogs.com/xwl3109377858/p/11271930.html

时间: 2024-10-06 00:04:54

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)的相关文章

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

D2. RGB Substring (hard version) 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 ss cons

Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在“RGBRGBRGBRGB........(以RGB为循环节,我们称这个串为str)”里面也是一个子串,这个子串的长度是k 可是有可能s字符串中找不到,那么这个时候就可以改变s字符串中某些位置的字母来完成任务.问最少需要改变多少个字母 题解: 主要看暴力的姿势对不对.在上一道的D1上面,我是对s字符串的每一个位置进行‘R’,‘G’,‘B’的枚举,因为如果这个子串也是str的子串的话

D2. RGB Substring (hard version)||D1. RGB Substring (easy version)

D2. RGB Substring (hard version)    原题传送门 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 strin

Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an undirected tree of nn vertices. Some vert

Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He

Codeforces Round #575 (Div. 3)记录

Codeforces Round #575 (Div. 3)记录 错过了上分的机会,上次不小心打了个div. 2结果直接掉了100多分. 我绿了,也变弱了.找下场Div. 3上上分吧. A 随便写了. 我的思路是三个东西先排序,一个人先拿最少的,另一个人拿次少的. 然后看剩下的能不能填补相差,如果能的话继续左右两边各补,补到剩1或0为止. 其实上面说这么多,答案就等于\(\lfloor \frac{a+b+c}{2} \rfloor\). 我是sb B 这些数与大小无关,我们直接统计有多少个奇数

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #575 (Div. 3)

本蒟蒻已经掉到灰名了(菜到落泪),希望这次打完能重回绿名吧...... 这次赛中A了三题 下面是本蒟蒻的题解 A.Three Piles of Candies 这题没啥好说的,相加除2就完事了 #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll q; scanf("%lld",&q); while(q--) { ll a,b,c; scanf("

Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)

C. Robot Breakout time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output n robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental,