题面

\(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串。

题解

一眼看过去可能是编辑距离什么的,但是仔细看 Hard 下的时间复杂度不允许,然后进行了一波分析...

上图模式串 2 同理。

从上图可以发现,其实就是主串往后移动一位的同时模式串也往后移动一位匹配,同时去掉无用信息即可。

代码

#include<cstdio>
#include<climits>
#include<algorithm>
#include<cstring>
const int MAXN=2e5+5;
int q,n,m,tot[3],v[3][2],value[MAXN],ans;char str[MAXN];
int main()
{
scanf("%d",&q);
for(;q>=1;q--)
{
memset(tot,0,sizeof(tot));
memset(v,0,sizeof(v));
ans=INT_MAX;
scanf("%d %d",&n,&m);
scanf("%s",str+1);
for(int i=0;i<3;i++)
v[i][0]=v[i][1]=i;
for(int i=1;i<=n;i++)
{
value[i]=(str[i]=='R')?0:(str[i]=='G'?1:2);
}
for(int i=1;i<=m;i++)
for(int j=0;j<3;j++)
{
if(value[i]==v[j][1])
{
tot[j]++;
}
v[j][1]=(v[j][1]+1)%3;
//printf("%d %d %d %d %d %d\n",i,j,v[j][0],v[j][1],value[j],tot[j]);
if(i==m)
{
ans=std::min(ans,m-tot[j]);
}
}
for(int i=m+1;i<=n;i++)
{
for(int j=0;j<3;j++)
{
if(value[i-m]==v[j][0])
{
tot[j]--;
}
if(value[i]==v[j][1])
{
tot[j]++;
}
v[j][1]=(v[j][1]+1)%3;
v[j][0]=(v[j][0]+1)%3;
ans=std::min(ans,m-tot[j]);
//printf("%d %d %d %d %d %d\n",i,j,v[j][0],v[j][1],value[j],tot[j]);
}
}
printf("%d\n",ans);
}
return 0;
}

Codeforces 1196D2 RGB Substring (Hard version) 题解的更多相关文章

  1. Codeforces 1196D2. RGB Substring (hard version)

    传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...

  2. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  3. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  4. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

  5. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”. ...

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

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

  7. CF1203D2 Remove the Substring (hard version) 题解

    这题初赛让我白给了6分,于是我决定回来解决一下它. 说实话,看原题题面和看CCF代码真是两种完全不同的感受…… ------------思路分析: 把$s$串删去一部分之后,会把$s$串分成两部分,当 ...

  8. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  9. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

随机推荐

  1. 巧用命令行工具 redis-cli

    我们天天都在使用 Redis 内置的命令行工具 redis-cli,久而久之以为它就是一个简单的交互式 Redis 数据结构手工操作程序,但是它背后强大的功能绝大多数同学可能闻所未闻.本节我们一起来挖 ...

  2. Stream:java1.8新特性

    原 Stream:java1.8新特性 2017年08月01日 18:15:43 kekeair-zhang 阅读数:392 标签: streamjava1-8新特性 更多 个人分类: 日记 版权声明 ...

  3. iOS开发之通过Framework建立项目依赖

    https://www.jianshu.com/p/d146db167bf3 项目开发时经常会遇到个别模块想打包成framework的情况,传统的打包方式太繁琐,反复测试时很麻烦,不如一个项目管理来得 ...

  4. 卸载mysql时,提示libmysqlclient.so.16()(64bit) is needed by (installed) postfix

    卸载时,提示错误,用这个命令就可以rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64(这个是自己要卸载的版本) [root@unaryhost dev]# ...

  5. C#多个泛型约束问题

    多个约束之间使用逗号隔开,但不重复T约束. 1. private void AddControl<T>(TabPage tabPage, T userControl) where T: U ...

  6. XCOJ 1008: 级数求和

    1008: 级数求和 题目描述 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K.现给出一个整数K(1<=k<=15),要求计算出一个最小 ...

  7. css3 :default应用场景

    引用自 张鑫旭文章.

  8. Git基本指令

    Git学习笔记 git //检查git是否安装 sudo apt-get install git git config --global user.name "dzq" git c ...

  9. 8.1.1默认的map函数、reduce函数、分区函数

    1.1.1         默认的map函数和reduce函数 (1)Maper和Reuducer默认类 如果没有指定maper类和reduce类,则会用默认的Maper和Reuducer类去处理数据 ...

  10. pytest框架,使用print在控制台输入

    代码示例: import pytest@pytest.fixture(autouse=True,scope='session') #autouse=True,scope='session'def se ...