575 div3RGB 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
BBBRROutput1
0
3
【题目分析】
题目要求给定串改变最少的字符使串的一个子串变成一个无限长串"RGBRGBRGB……""RGBRGBRGB……""RGBRGBRGB……"的一个子串
我自己看完后十分懵逼,因为没什么想法,既没有说改变的是哪一部分子串,也没有说变成什么样子,还需要最小。在看到别人的博客以后才恍然大悟,我们不必要将目光局限在改变的那一小部分子串上,我们如果将最终改变的子串拓展成和给定串长度相同的串,无外乎三种,分别以RRR、GGG、BBB开头,我们就分别计算如果将串变成那三种样子哪些位置需要改变(需要改变的位置记为1,不需要改变的位置记为0,然后用一个前缀和数组进行维护),然后再找长度为k的子串中需要改变最少的。
觉得自己这种抽象能力不太够,还需要多进行锻炼。
【AC代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<climits>
#include<cstdlib>
#include<cmath>using namespace std;typedef long long ll;const int MAXN=200005;
char s[MAXN];
const char t[4]="RGB";
int sum[MAXN][3];
int ans;int main()
{int T,n,k;scanf("%d",&T);while(T--){scanf("%d%d",&n,&k);scanf("%s",s);ans=INT_MAX;//memset(sum,0,sizeof(sum));sum[0][0]=sum[0][1]=sum[0][2]=0;for(int i=0;i<n;i++){for(int j=0;j<3;j++){if(s[i]!=t[(i+j)%3]){sum[i+1][j]=sum[i][j]+1;}else{sum[i+1][j]=sum[i][j];}}}for(int i=0;i<=n-k;i++){for(int j=0;j<3;j++){ans=min(ans,sum[i+k][j]-sum[i][j]);}}printf("%d\n",ans);}return 0;
}
575 div3RGB Substring (hard version)——思维-相关推荐
- CodeForces - 1559D2 Mocha and Diana (Hard Version)(思维)
题目链接:点击查看 题目大意:给出两棵森林,每次可以同时在两个森林中增加同一条边,问最多可以增加多少条边,使得两个森林仍然还是森林 题目分析:结论参考至:https://blog.csdn.net/R ...
- Codeforces Round #617 (Div. 3) E2. String Coloring (hard version) 思维 + dp + Dilworth定理
传送门 文章目录 题意: 思路: 题意: 让你给一个串染色,不同颜色且相邻的一对字符可以互换位置,用最少的颜色,使交换后这个字符串字典序最小. 思路: 考虑将字符串分成若干个非递减的子序列,由于其非递 ...
- C2 - Pokémon Army (hard version)(思维+差分/线段树+dp)详解
https://codeforces.com/contest/1420/problem/C2 这道题十分的锻炼思维,也让我知道了同样是差分,从前面减后面和从后面减前面是有不同的意义的. 还记得c1吗? ...
- 【LeetCode】No.5. Longest Palindromic Substring -- Java Version
题目链接: https://leetcode.com/problems/longest-palindromic-substring/ 1. 题目介绍(最长回文子串) Given a string s, ...
- Balanced Substring CodeForces - 873B (思维+前缀和)
Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...
- [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 &quo ...
- CodeForces - 1450C2 Errich-Tac-Toe (Hard Version)(思维+构造)
题目链接:点击查看 题目大意:给出一个大小为 n * m 的棋盘,规定不能有三个连续的 ' X ' 或三个连续的 ' O ' ,现在可以通过一次操作将 ' X ' 改成 ' O ' 或者将 ' O ' ...
- CodeForces - 1335E2 Three Blocks Palindrome (hard version)(思维)
题目链接:点击查看 题目大意:给出 three blocks palindrome 的定义,为 aa....aa + bb....bb + aa....aa 三个部分组成的字符串,其中第一段为 x , ...
- Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) 思维 + 质因子
传送门 文章目录 题意: 思路: 题意: 大体题意跟easyeasyeasy版本差不多,就是hardhardhard版本的aaa范围更大.见这里Codeforces Round #601 (Div. ...
最新文章
- 【实验】配置DHCP和NAT访问Internet公网案例
- C++ StrCat()
- 自动摘要php,修改DEDECMS文章自动摘要长度或者取掉文章摘要
- 基于MLlib的机器学习--协同过滤与推荐
- python嵌套列表索引 index_Python:嵌套lis中元素的索引列表
- Linux netfilter源码分析(5)
- 机器学习基础-线性代数学习
- 微信小程序列表切换样式简单案例
- 无线数字信息传送服务器,无线数字远程监控管理及网站实时推广项目方案.doc...
- window操作系统快捷键
- 【计算机网络】第三部分 数据链路层(18) 虚电路网络:帧中继和ATM
- win10 微信/QQ等能听到别人说话,别人听不到自己说话解决方案
- Redis-keys命令
- 鸡蛋,必须放在合适的篮子里
- 简要分析“荒野乱斗”基本元素
- 刷题记录(NC235611 牛牛国的战争,NC23803 DongDong认亲戚,NC235622 叠积木)
- 51单片机 8x8LED点阵屏循环显示数字0~9
- 如何正确的看待人工智能?只有编程基础的人可以学吗?
- c++----随机数算法
- IT项目管理小组分工情况
热门文章
- 【目录】《剑指Offer》Java实现
- 1034. 二哥的金链
- Android 上下文菜单(Context Menu)
- 用C#来学习唐诗三百首和全唐诗
- 分层设计 --java中的几种包
- POJ 1745 Divisibility【DP】
- Automatic Reference Counting
- 二分法在数组内查找数c语言,C++二分法在数组中查找关键字的方法
- linux 安装ftp下载,LINUX FTP安装与配置
- zabbix安装MySQL失败_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...