D1. RGB Substring (easy version)

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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

inputCopy

3

5 2

BGGGG

5 3

RBRGR

5 5

BBBRR

outputCopy

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

题意:

两题只是数据范围不同,所以放在一起讲解,给你一个字符串和一个k,问你最少改变多少个字符可以使字符串的一个k长度子串是RGBRGBRGB***** 无限长的子串?

思路:

D1: 时间复杂度显然可以O(n*k) 来做

我们先构造3个k长度的RGB字符串,分别以 R,G,B 开头。

然后去O(nk)枚举字符串的所有k长度的连续子串O(3K)的扫描这个子串和3个GBR字符串的不同字符个数,

维护最小的不同个数就是题目答案。

D2 : 时间复杂度O(N+K)是可以接受的。

那么我们先构造3个长度为n的RGB字符串,让每一个都和字符串str继续匹配。

匹配时我们维护a[i] 代表第i个字符串是否与RGB字符串不同,不同为1,相同为0.

然后求a[i]的前缀和,然后O(N)扫一遍处理长度为k的字符串最少有多少个与RGB字符串的不同值。

两份代码:第一个是D1,第二个是D2

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout); int q;
int n,k;
gbtb;
cin>>q;
while(q--)
{
cin>>n>>k;
cin>>s;
string str1,str2,str3;
str1="";
str2="";
str3="";
for(int i=0;i<k;++i)
{
if(i%3==0)
{
str1.pb('R');
}else if((i%3)==1)
{
str1.pb('G');
}
else
{
str1.pb('B');
}
}
for(int i=0;i<k;++i)
{
if(i%3==0)
{
str2.pb('G');
}else if((i%3)==1)
{
str2.pb('B');
}
else
{
str2.pb('R');
}
}
for(int i=0;i<k;++i)
{
if(i%3==0)
{
str3.pb('B');
}else if((i%3)==1)
{
str3.pb('R');
}
else
{
str3.pb('G');
}
}
int ans=inf;
for(int i=0;i+k-1<n;++i)
{
int cnt=0;
for(int j=i;j<=i+k-1;++j)
{
if(s[j]!=str1[j-i])
{
cnt++;
}
}
ans=min(ans,cnt);
cnt=0;
for(int j=i;j<=i+k-1;++j)
{
if(s[j]!=str2[j-i])
{
cnt++;
}
}
ans=min(ans,cnt);
cnt=0;
for(int j=i;j<=i+k-1;++j)
{
if(s[j]!=str3[j-i])
{
cnt++;
}
}
ans=min(ans,cnt);
cnt=0;
}
cout<<ans<<endl;
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int a[maxn];
int sum[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout); int q;
int n,k;
gbtb;
cin>>q;
while(q--)
{
cin>>n>>k;
cin>>s;
string str1,str2,str3;
str1="";
str2="";
str3="";
for(int i=0;i<n;++i)
{
if(i%3==0)
{
str1.pb('R');
}else if((i%3)==1)
{
str1.pb('G');
}
else
{
str1.pb('B');
}
}
for(int i=0;i<n;++i)
{
if(i%3==0)
{
str2.pb('G');
}else if((i%3)==1)
{
str2.pb('B');
}
else
{
str2.pb('R');
}
}
for(int i=0;i<n;++i)
{
if(i%3==0)
{
str3.pb('B');
}else if((i%3)==1)
{
str3.pb('R');
}
else
{
str3.pb('G');
}
}
int ans=inf;
rep(i,0,n)
{
if(s[i]!=str1[i])
{
a[i]=1;
}else
{
a[i]=0;
}
}
sum[0]=a[0];
rep(i,1,n)
{
sum[i]=sum[i-1]+a[i];
}
for(int i=k-1;i<n;++i)
{
int temp=0;
if(i-k>=0)
temp=sum[i-k];
ans=min(ans,sum[i]-temp);
}
rep(i,0,n)
{
if(s[i]!=str2[i])
{
a[i]=1;
}else
{
a[i]=0;
}
}
sum[0]=a[0];
rep(i,1,n)
{
sum[i]=sum[i-1]+a[i];
}
for(int i=k-1;i<n;++i)
{
int temp=0;
if(i-k>=0)
temp=sum[i-k];
ans=min(ans,sum[i]-temp);
}
rep(i,0,n)
{
if(s[i]!=str3[i])
{
a[i]=1;
}else
{
a[i]=0;
}
}
sum[0]=a[0];
rep(i,1,n)
{
sum[i]=sum[i-1]+a[i];
}
for(int i=k-1;i<n;++i)
{
int temp=0;
if(i-k>=0)
temp=sum[i-k];
ans=min(ans,sum[i]-temp);
}
cout<<ans<<endl;
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)的更多相关文章

  1. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  2. Codeforces Round #575 (Div. 3) 昨天的div3 补题

    Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...

  3. Codeforces Round #575 (Div. 3)

    本蒟蒻已经掉到灰名了(菜到落泪),希望这次打完能重回绿名吧...... 这次赛中A了三题 下面是本蒟蒻的题解 A.Three Piles of Candies 这题没啥好说的,相加除2就完事了 #in ...

  4. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  5. Codeforces Round #527 (Div. 3) D1. Great Vova Wall (Version 1) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D1 D1. Great Vova Wall (Version 1) time limit per tes ...

  6. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  7. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

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

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

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

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

随机推荐

  1. golang 开源项目: 配置解析模块--config

    在golang中,配置文件经常使用json格式.json格式的语法,有些繁琐,尤其是出现嵌套的时候,每一块都需要大括号包裹,看起来很臃肿. 本着简单易用的原则,个人开发了一个配置解析模块config, ...

  2. CAN-FD协议浅析

    引言 随着电子.半导体.通讯等行业的快速发展,汽车电子智能化的诉求也越来越强,消费者希望驾驶动力性.舒适性.经济性以及娱乐性更强的汽车.汽车制造商为了提高产品竞争力,将越来越多的电子控制系统加入到汽车 ...

  3. 国际标准 ISO 15008

    Road vehicles — Ergonomic aspects of transport information and control systems — Specifications and ...

  4. 代码实现:判断101-200之间有多少个素数(质数),并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

    package com.loaderman.Coding; /* 判断101-200之间有多少个素数(质数),并输出所有素数. 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能 ...

  5. Nodejs 之非阻塞 I/O、异步、事件驱动

    1.非阻塞 I/O var fs = require('fs'); console.log('); fs.readFile('mime.json',function (err,data) {//rea ...

  6. 63不同路径II

    题目: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”).现在考 ...

  7. linux LVM分区查看dm设备

    linux LVM分区查看dm设备 在linux中iostat -d查看磁盘状态时,有的会有如下dm-0,dm-1的条目. Device:            tps    kB_read/s   ...

  8. 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray

    在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...

  9. Java锁机制ReentrantLock

    ReentrantLock 锁常用于保证程序的人为顺序执行. 写一个类模拟ReentrantLock类的功能 class MyLock{ private boolean lock = false; p ...

  10. 关于java中设计原则总结(7)

    开闭原则: 对于类,模块函数等扩展要开放,对于修改要关闭. 依赖倒置: 接口或抽象是高层,要面向高层编程,不应该面向实现类(实现类是低层)去变成. 单一职责: 对一个类,或者一个功能,只用负责一个职责 ...