kZjPBD.jpg

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

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
Copy
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
output
Copy
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".

SLUTION:

这题比赛的时候有两个版本,easy版的直接暴力看有多少配的的字符就行了

hard 版本的n到达了2e5,我们可以使用fft来优化字符匹配的过程 复杂度o(3*n*Logn)

 

CODE:

#include"bits/stdc++.h"
#define sd(x) scanf("%lf",&(x));
#define sld(x) scanf("%lld",&(x));
using namespace std;

const int maxn = 2e6+10;
const double Pi = acos(-1.0);

struct cp
{
    double x,y;
    cp (double xx=0,double yy=0)
    {
        x=xx,y=yy;
    }
} a[maxn],b[maxn];
cp operator + (cp a,cp b)
{
    return cp(a.x+b.x , a.y+b.y);
}
cp operator - (cp a,cp b)
{
    return cp(a.x-b.x , a.y-b.y);
}
cp operator * (cp a,cp b)
{
    return cp(a.x*b.x-a.y*b.y , a.x*b.y+a.y*b.x);   //不懂的看复数的运算那部分
}

int n,k;
int l,r[maxn];
int limit = 1;


inline void fft(cp *a,int ff)
{
    for(int i=0; i<limit; i++)
        if(i<r[i])swap(a[i],a[r[i]]);
    for(int mid=1; mid<limit; mid<<=1)
    {
        cp wn(cos(Pi/mid) , ff*sin(Pi/mid));
        for(int R=mid<<1,j=0; j<limit; j+=R)
        {
            cp w(1,0);
            for(int k=0; k<mid; k++,w=w*wn)
            {
                cp x=a[j+k],y=w*a[j+mid+k];
                a[j+k]=x+y;
                a[j+mid+k]=x-y;
            }
        }

    }


}
char s[2000000];
char t[2000000];
int tot;
int ans;
char ff[]= {'R','G','B'};
int tmp[2000000];
void work()
{
    int m=tot;
    limit=1;
    l=0;
    reverse(t,t+m);
    while(limit<=n+m)limit<<=1,l++;
    for(int i=0; i<limit; i++)
        r[i]=(r[i>>1]>>1)|( (i&1)<<(l-1));

    for(int j=m-1; j<m+n-k; j++) tmp[j]=0;

    for(int o=0; o<3; o++)
    {
        for(int j=0; j<limit; j++)a[j].x=a[j].y=b[j].x=b[j].y=0;
        for(int j=0; j<m; j++)a[j].x=(t[j]==ff[o]);
        for(int j=0; j<n; j++)b[j].x=(s[j]==ff[o]);
        fft(a,1);
        fft(b,1);
        for(int j=0; j<limit; j++)a[j]=a[j]*b[j];
        fft(a,-1);

        for(int j=m-1; j<m+n-k; j++)
            tmp[j] += int(a[j].x/limit + 0.5);


    }
    for(int j=m-1; j<m+n-k; j++)
        ans=min(ans,k-tmp[j]);



}

int main()
{


    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        cin>>s;
        ans=1e9;
        tot=k;
        for(int i=0; i<k; i++)
            t[i]=ff[(i)%3];
        work();
        for(int i=0; i<k; i++)
            t[i]=ff[(i+1)%3];
        work();
        for(int i=0; i<k; i++)
            t[i]=ff[(i+2)%3];
        work();
        cout<<ans<<endl;
    }
}

  

 

 

 

posted @ 2019-07-25 09:03  Through_The_Night  阅读(345)  评论(0编辑  收藏  举报