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 string ss consisting of nn characters, each character is 'R', 'G' or 'B'.

You are also given an integer kk. Your task is to change the minimum number of characters in the initial string ss so that after the changes there will be a string of length kk that is a substring of ss, and is also a substring of the infinite string "RGBRGBRGB ...".

A string aa is a substring of string bb if there exists a positive integer ii such that a1=bia1=bi, a2=bi+1a2=bi+1, a3=bi+2a3=bi+2, ..., a|a|=bi+|a|1a|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 qq independent queries.

Input

The first line of the input contains one integer qq (1q21051≤q≤2⋅105) — the number of queries. Then qq queries follow.

The first line of the query contains two integers nn and kk (1kn21051≤k≤n≤2⋅105) — the length of the string ss and the length of the substring.

The second line of the query contains a string ss consisting of nn characters 'R', 'G' and 'B'.

It is guaranteed that the sum of nn over all queries does not exceed 21052⋅105 (n2105∑n≤2⋅105).

Output

For each query print one integer — the minimum number of characters you need to change in the initial string ss so that after changing there will be a substring of length kk in ss 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".

题意:

有个“RGB”无限循环的长母串,q组样例,每组一个n长的字符串,让你通过更改n长串的某个字符构造一个长度为k的,既是母串的子串,也是n长串的子串。求满足要求的最少更改次数。

思路:

母串t:BGGGG(3种)
样串s:RBGRBGRBGRBGRBGRBG
贡献a:11011 (不同的话,在这一位的贡献就是1,这个也是3种)

然后因为的们要获得区间长度为K的最小贡献,所以求个a的前缀和
O(1)遍历每一个长度为K的子串,找出最小的那个就是ans

 

#include<iostream>
#include<cstring>
using namespace std;
const int M=2e5+10;
char s[M];
int a[3][M];
int T,n,k,ans; int main() { cin>>T; while(T--) { a[0][0]=a[1][0]=a[2][0]=0; cin>>n>>k; cin>>s; ans=M; string t="RGB"; for(int i=0; i<n; i++) { for(int j=0;j<=2;j++){ if(s[i]!=t[(i+j)%3])a[j][i+1]=a[j][i]+1; else a[j][i+1]=a[j][i]; } }//a从下标为1的位开始&前缀和 for(int i=0; i+k<=n; i++) { for(int j=0;j<=2;j++){ ans=min(ans,a[j][i+k]-a[j][i]); } } //遍历求ans cout<<ans<<endl; } return 0; }

 

 

 

  

posted on 2019-07-26 00:20  GeraldG 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/geraldg/p/11247810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public static String wuxing(String gzYear, String gzMonth, String gzDay, String gzTime) { // 天干年 String gYear = gzYear.substring(0, 1); if ("甲".equals(gYear) || "乙".equals(gYear)) { gYear = "木"; } if ("丙".equals(gYear) || "丁".equals(gYear)) { gYear = "火"; } if ("戊".equals(gYear) || "己".equals(gYear)) { gYear = "土"; } if ("庚".equals(gYear) || "辛".equals(gYear)) { gYear = "金"; } if ("壬".equals(gYear) || "癸".equals(gYear)) { gYear = "水"; } // 天干月 String gMonth = gzMonth.substring(0, 1); if ("甲".equals(gMonth) || "乙".equals(gMonth)) { gMonth = "木"; } if ("丙".equals(gMonth) || "丁".equals(gMonth)) { gMonth = "火"; } if ("戊".equals(gMonth) || "己".equals(gMonth)) { gMonth = "土"; } if ("庚".equals(gMonth) || "辛".equals(gMonth)) { gMonth = "金"; } if ("壬".equals(gMonth) || "癸".equals(gMonth)) { gMonth = "水"; } // 天干日 String gDay = gzDay.substring(0, 1); if ("甲".equals(gDay) || "乙".equals(gDay)) { gDay = "木"; } if ("丙".equals(gDay) || "丁".equals(gDay)) { gDay = "火"; } if ("戊".equals(gDay) || "己".equals(gDay)) { gDay = "土"; } if ("庚".equals(gDay) || "辛".equals(gDay)) { gDay = "金"; } if ("壬".equals(gDay) || "癸".equals(gDay)) { gDay = "水"; } // 天干时 String gTime = gzTime.substring(0, 1); if ("甲".equals(gTime) || "乙".equals(gTime)) { gTime = "木"; } if ("丙".equals(gTime) || "丁".equals(gTime)) { gTime = "火"; } if ("戊".equals(gTime) || "己".equals(gTime)) { gTime = "土"; } if ("庚".equals(gTime) || "辛".equals(gTime)) { gTime = "金"; } if ("壬".equals(gTime) || "癸".equals(gTime)) { gTime = "水"; }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值