Codeforces-1196-D. RGB Substring(滑动窗口)

D2. RGB Substring (hard 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≤2⋅105) — the number of queries. Then q queries follow.

The first line of the query contains two integers n and k (1≤k≤n≤2⋅105) — 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 2⋅105 (∑n≤2⋅105).

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‘ ’G‘ ’B’ ,开一个二维数组,记录更改的开销,如果是他本身那么开销为0,其余两个都为1。初始化完了之后,用一个长度为K的滑动窗口,右边收录一个字符,加上对应的开销,而左边剔除一个字符,减去对应的开销。同样是要枚举三种情况,每一次移动都结算一次最小值,最后得到的就是全局最优解。那么这个窗口在初始化的时候,首先需要收录K个字符,先记录下他们的开销,以及开始和结束位置,接下来遍历整个串就行了,复杂度O(3*n)。

AC代码:

#include <bits/stdc++.h>

const int N = 1e6+10;
using namespace std;
int a[N][3]; // 记录每个字符的开销
map<char,int> mp; // 把字符映射成数字,便于处理

int main()
{
    int n,m;
    mp['R'] = 0;mp['G'] = 1;mp['B'] = 2;
    string s;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>s;
        for(int i = 0 ; i < n ; i ++ ) // 初始化开销数组
            a[i][mp[s[i]]] = 0;a[i][(mp[s[i]]+1)%3] = 1;a[i][(mp[s[i]]+2)%3] = 1;
        int head1,head2,head3;
        int tail1,tail2,tail3;
        int sum1,sum2,sum3;
        head1 = 0 ; head2 = 1 ; head3 = 2 ;
        tail1 = 0 ; tail2 = 1 ; tail3 = 2 ;
        sum1 = sum2 = sum3 = 0;
        for(int i = 0 ; i < m ; i ++) // 初始化长度为K的窗口
        {
            sum1 += a[i][head1];
            sum2 += a[i][head2];
            sum3 += a[i][head3];
            head1 = (head1+1)%3;
            head2 = (head2+1)%3;
            head3 = (head3+1)%3;
        }
        int ans = min(sum1,min(sum2,sum3));
        for(int i = m ; i < n ; i ++) // 遍历数组,更新最小值
        {
            sum1 += a[i][head1];
            sum2 += a[i][head2];
            sum3 += a[i][head3];
            sum1 -= a[i-m][tail1];
            sum2 -= a[i-m][tail2];
            sum3 -= a[i-m][tail3];
            head1 = (head1+1)%3;
            head2 = (head2+1)%3;
            head3 = (head3+1)%3;
            tail1 = (tail1+1)%3;
            tail2 = (tail2+1)%3;
            tail3 = (tail3+1)%3;
            ans = min(min(ans,sum1),min(sum2,sum3));
        }
        cout << ans << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值