RGB Substring (hard version) (CF-1196D2)
Problem Description
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=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 qq independent queries.
Input
The first line of the input contains one integer q (1≤q≤2000) — the number of queries. Then qq queries follow.
The first line of the query contains two integers nn and k (1≤k≤n≤2000) — 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 2000 (∑n≤2000.
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 ...".
Examples
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRROutput
1
0
3
题意:q 组询问,每组给出一个长度为 n 的字符串与一个 "RGBRGBRGB..." 循环子串的长度 k,现在要所给的长度为 n 的子串中选择一个长度为 k 的子串,对其进行任意次代价为 1 的更改,即可将任意一个字符改为其他字符,使得这个子串与 "RGBRGBRGB..." 循环子串所匹配,求最小代价
思路:
RGB Substring (easy version) (CF-1196D1)的数据加强版
由于数据范围扩大到 2E5,使用暴力会 TLE,使用前缀和来解决
对于给出 n,k,首先构造一个长度为 n+3 的 "RGBRGBRGB..." 循环子串,然后对于三种固定的开头:R、G、B 分别进行三次枚举
每次枚举将构造的子串与给出的字符串进行匹配,即 t[i+j-1]=a[j],其中 i 代表 R、G、B 三种固定开头,j 代表给出字符串的第 j 个字符,如果匹配为记为 1,如果不匹配记为 0,这样即得出当前开头的匹配情况,然后对匹配情况求一个前缀和
对前缀和进行从 k 枚举到 n,将 sum[j] 与 sum[j-kl] 做差,即得到从第 j 个位置到第 j+k 个位置的匹配代价,然后求所有匹配代价的最小值即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
char str[N];
char t[N];
int a[N];
int sum[N];
int main(){
int q;
scanf("%d",&q);
while(q--){
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",str+1);
for(int i=1;i<=n+3;i+=3){
t[i]='R';
t[i+1]='G';
t[i+2]='B';
}
int res=INF;
for(int i=1;i<=3;i++){
for(int j=1;j<=n;j++){
if(t[j+i-1]==str[j])
a[j]=0;
else
a[j]=1;
}
for(int j=1;j<=n;j++)
sum[j]=sum[j-1]+a[j];
for(int j=k;j<=n;j++)
res=min(res,sum[j]-sum[j-k]);
}
printf("%d\n",res);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)