Codeforces 7-24 div3 RGB Substring

RGB Substring (easy version)

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

problem

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

题意

您将得到一个由n个字符组成的字符串s,每个字符都是“r”、“g”或“b”。

还为您提供了一个整数k。您的任务是更改初始字符串s中的最小字符数,以便更改后将有一个长度为k的字符串,该字符串是s的子字符串,也是无限字符串“rgbrgbrgbb…”的子字符串。

如果存在正整数i(a1=bi,a2=bi+1,a3=bi+2,…,a a=bi+a−1),则字符串a是字符串b的子字符串。例如,字符串“gbrg”、“b”、“br”是无限字符串“rgbrgbrgb…”的子字符串,而“gr”、“rgr”和“ggg”则不是。

分析

要让原字符串中出现长度为 K 的“rgbrgbrgbb…”的子字符串,而这个字符串又是一个循环字符串,它又可以看成 “rgb” 、“gbr”或“brg” 循环形成的,所以要使原字符串中含有长度为 k 的字符串 ,只需要从第一个字符遍历使得它们符合这三种子串的排布,然后找到改动最小的哪一种

代码

#include<bits/stdc++.h>
using namespace std;
char c[3]={'R','G','B'};
int main(){
	int q;  
	cin>>q;
	while(q--){
		int n,k;
		cin>>n>>k;
		int ss=1<<30;
		string s;
		cin>>s;
		for(int i=0;i<s.size()-k+1;i++){
			int ss1=0,ss2=0,ss3=0;
			for(int j=0;j<k;j++){
				if(s[i+j]!=c[j%3])		ss1++; 	// RGB 
				if(s[i+j]!=c[(j+1)%3])	ss2++;	// GBR
				if(s[i+j]!=c[(j+2)%3])	ss3++;	// BRG
			}
			ss=min(min(ss,ss1),min(ss2,ss3)); // 找出最小的改动
		}
		cout<<ss<<endl;
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值