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
q1≤q≤2105q
nk1≤k≤n≤2105s
snR', 'G' and 'B'.
n2105∑n≤2105
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; } 来源:博客园
作者:就很甜呀
链接:https://www.cnblogs.com/Shallow-dream/p/11416735.html