zoukankan      html  css  js  c++  java
  • Codeforces Round #575 (Div. 3) D2

    Codeforces Round #575 (Div. 3)

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

     

    题意:题意见上一篇 D1( easy version) ,2333

    思路:用上一篇D1暴力写法,显然 TLE ,那么我们如何来优化算法呢,

    可以看下上一篇暴力的写法,每一次都是截取出字符串重新去匹配三遍,其实这里显然重复匹配了,

    那我们就可以在匹配时保存匹配的状态,然后每次匹配完,更新答案,往后挪动一个位置,

    加上后一个字符状态,减去前面一个字符匹配状态,大概就是尺取法的思想。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<map>
     6 #include<set>
     7 #include<vector>
     8 #include<algorithm>
     9 #include<queue>
    10 #include<unordered_map>
    11 using namespace std;
    12 #define ll long long 
    13 const int mod=1e9+7;
    14 const int inf=1e9+7;
    15  
    16 const int maxn=2e5+10;
    17  
    18 int res[maxn];
    19  
    20 int main()
    21 {
    22     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    23     
    24     const string t="RGB";
    25     
    26     int T;
    27     cin>>T;
    28     
    29     int n,k;
    30     string str;
    31     
    32     while(T--)
    33     {
    34         cin>>n>>k>>str;
    35         
    36         int minn=inf;
    37         
    38         for(int flag=0;flag<3;flag++)//三个不同RGB开始顺序匹配 
    39         {
    40             
    41             int cnt=0;
    42             
    43             for(int i=0;i<k;i++)
    44             {
    45                 if(str[i]!=t[(i+flag)%3])
    46                     cnt++;
    47             }
    48             
    49             if(cnt<minn)
    50                 minn=cnt;//刷新最小值 
    51             
    52             for(int i=k;i<n;i++)
    53             {
    54                 if(str[i]!=t[(i+flag)%3])//处理最后一个字符 
    55                     cnt++;
    56                 if(str[i-k]!=t[(i-k+flag)%3])//处理最前面一个字符 
    57                     cnt--;
    58                 
    59                 if(cnt<minn)
    60                     minn=cnt;//刷新最小值 
    61             }
    62             
    63         }
    64         
    65         cout<<minn<<endl;
    66     }
    67     
    68     return 0;
    69 }
    大佬见笑,,
  • 相关阅读:
    【随感】我真的很敬重你,只是你不知道。。。。。
    【diary】智慧的贪婪
    【随感】看老科和当当的回眸相视
    【转载】清华毕业致辞:从容行走于无人知晓的荒原,欣赏并创造别样的风景
    【随感】不要以为自己不足轻重而放任自己做一些事或一些话。你的不在意,才会影响到别人也不在意你。
    【随感】我觉得,世界上最美好的乐器是钢琴和架子鼓
    【技术】HTML5 canvas clock(1)
    【随感】youngLaker,fighting!
    转发公司招聘
    DBCC DBREINDEX
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11271989.html
Copyright © 2011-2022 走看看