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

匿名 (未验证) 提交于 2019-12-02 23:55:01

The only difference between easy and hard versions is the size of the input.

snR', 'G' or 'B'.

kskss, and is also a substring of the infinite string "RGBRGBRGB ...".

abia1=bia2=bi+1a3=bi+2a|a|=bi+|a|1GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.

q

Input

q1q2105q

nk1kn2105s

snR', 'G' and 'B'.

n2105n2105

Output

sksRGBRGBRGB ...".

#include<bits/stdc++.h> using namespace std;   const int maxn = 2e5+7; int n,k; string ori = "RGB"; string s; int a[maxn],sum[maxn]; int solve(int st) {     for(int i=0; i<s.size(); i++)     {         a[i]=(s[i]==ori[(st+i)%3]?0:1);         sum[i]=(i>0?a[i]+sum[i-1]:a[i]);     }     int res = s.size();     for(int i=k-1; i<s.size(); i++)     {         res = min(res, sum[i]-(i-k>=0?sum[i-k]:0));     }     return res; } void solve() {     scanf("%d%d",&n,&k);     cin>>s;     int ans = s.size();     for(int st=0; st<3; st++)     {         ans = min(ans, solve(st));     }     printf("%d\n", ans); } int main() {     int t;     scanf("%d",&t);     while(t--)     {         solve();     }     return 0; }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!