传送门

题意:

给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串。使得这个子串在“RGBRGBRGBRGB........(以RGB为循环节,我们称这个串为str)”里面也是一个子串,这个子串的长度是k

可是有可能s字符串中找不到,那么这个时候就可以改变s字符串中某些位置的字母来完成任务。问最少需要改变多少个字母

题解:

主要看暴力的姿势对不对。在上一道的D1上面,我是对s字符串的每一个位置进行‘R’,‘G’,‘B’的枚举,因为如果这个子串也是str的子串的话,那么肯定是以‘GB’,‘B’,“RGB”这三个为开头后面都是RGB

 1 if(s[i]!='G') sum++;//,printf("1\n");
2 if(s[i+1]!='B') sum++;//,printf("2\n");
3 for(int k=0,j=i+2; j<i+m; ++j,k++,k%=3)
4 //因为str字符串是以RGB循环,所以k就是来表示现在是R、G、B中的谁
5
6 if(s[i]!='B') sum++;//,printf("6\n");
7 //if(s[i+1]!='B') sum++;
8 for(int k=0,j=i+1; j<i+m; ++j,k++,k%=3)
9
10
11
12 //if(s[i]!='B') sum++,printf("6\n");
13 //if(s[i+1]!='B') sum++;
14 for(int k=0,j=i; j<i+m; ++j,k++,k%=3)

就是这样先枚举字符串s的每一个位置,再枚举开头的第一个字母(‘R’、‘G’、‘B’)

但是T了 T_T

T代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=2e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 char s[maxn];
14 int main()
15 {
16 int t;
17 scanf("%d",&t);
18 while(t--)
19 {
20 int n,m,ans=INF,sum;
21 scanf("%d%d",&n,&m);
22 scanf("%s",s);
23 if(m==1)
24 {
25 printf("0\n");
26 continue;
27 }
28 for(int i=0; i<n; ++i)
29 {
30 if(i+m<=n)
31 {
32 sum=0;
33 if(s[i]!='G') sum++;//,printf("1\n");
34 if(s[i+1]!='B') sum++;//,printf("2\n");
35 for(int k=0,j=i+2; j<i+m; ++j,k++,k%=3)
36 {
37 if(k==0)
38 {
39 if(s[j]!='R') ++sum;//,printf("3\n");
40 }
41 else if(k==1)
42 {
43 if(s[j]!='G') ++sum;//,printf("4\n");
44 }
45 else if(k==2)
46 {
47 if(s[j]!='B') ++sum;//,printf("5\n");
48 }
49 }
50 ans=min(sum,ans);
51 sum=0;
52 if(s[i]!='B') sum++;//,printf("6\n");
53 //if(s[i+1]!='B') sum++;
54 for(int k=0,j=i+1; j<i+m; ++j,k++,k%=3)
55 {
56 if(k==0)
57 {
58 if(s[j]!='R') ++sum;//,printf("7\n");
59 }
60 else if(k==1)
61 {
62 if(s[j]!='G') ++sum;//,printf("8\n");
63 }
64 else if(k==2)
65 {
66 if(s[j]!='B') ++sum;//,printf("9\n");
67 }
68 }
69 ans=min(sum,ans);
70 //printf("%d %d\n",sum,i);
71 sum=0;
72 //if(s[i]!='B') sum++,printf("6\n");
73 //if(s[i+1]!='B') sum++;
74 for(int k=0,j=i; j<i+m; ++j,k++,k%=3)
75 {
76 if(k==0)
77 {
78 if(s[j]!='R') ++sum;//,printf("77\n");
79 }
80 else if(k==1)
81 {
82 if(s[j]!='G') ++sum;//,printf("88\n");
83 }
84 else if(k==2)
85 {
86 if(s[j]!='B') ++sum;//,printf("99\n");
87 }
88 }
89 ans=min(sum,ans);
90 }
91 else
92 {
93 break;
94 }
95 }
96 printf("%d\n",ans);
97 }
98 return 0;
99 }

除了这样外,还可以先枚举k(开头第一个字母),然后再枚举每一个字符串s的位置

这样也把所有情况包含了,具体看代码

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<map>
6 #include<math.h>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=2e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 char s[maxn],num[maxn];
14 int check(int status,int position) //就是看一下这个位置匹配成不成功,需不需要改变这个位置的字符
15 {
16 if(status==0 && s[position]=='R')
17 return 0;
18 else if(status==1 && s[position]=='G')
19 return 0;
20 else if(status==2 && s[position]=='B')
21 return 0;
22 return 1;
23 }
24 int main()
25 {
26 int t;
27 scanf("%d",&t);
28 while(t--)
29 {
30 int n,m,ans=INF,sum;
31 scanf("%d%d",&n,&m);
32 scanf("%s",s);
33 if(m==1)
34 {
35 printf("0\n");
36 continue;
37 }
38 for(int status=0;status<3;++status) //枚举开头第一个字母
39 {
40 sum=0;
41 memset(num,0,sizeof(num));
42 for(int i=1;i<=n;++i) //这里和上一个TLE的代码不一样,这里是枚举到了n
43 { //只有这样在status变化的时候才会把所有情况包含,可以实践一下!
44 num[i]=check((status+i-1)%3,i-1);
45 sum+=num[i];
46 if(i>=m)
47 sum-=num[i-m];
48 if(i>=m)
49 ans=min(ans,sum);
50 }
51 }
52 printf("%d\n",ans);
53 }
54 return 0;
55 }

Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)的更多相关文章

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

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

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

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

  3. Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per tes ...

  4. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  5. Codeforces Round #575 (Div. 3) 昨天的div3 补题

    Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #575 (Div. 3)

    本蒟蒻已经掉到灰名了(菜到落泪),希望这次打完能重回绿名吧...... 这次赛中A了三题 下面是本蒟蒻的题解 A.Three Piles of Candies 这题没啥好说的,相加除2就完事了 #in ...

  8. Codeforces Round #575 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1196 A. Three Piles of Candies 题意:两个人分三堆糖果,两个人先各拿一堆,然后剩下一堆随意分配,使两个人 ...

  9. 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 ...

随机推荐

  1. 【Java】Java关键字、含义

    Java关键字 来自 Java 核心技术卷I 基础知识(原书第10 版)/( 美)凯S 霍斯特曼(Cay S . Horstmann )著: 周立新等译一北京:机械工业出版社, 2016 . 8 Ja ...

  2. 【RAC】安装cluster软件 在节点2执行root.sh脚本

    安装cluster软件  在节点2执行root.sh脚本 报错如下: Running vipca(silent) for configuring nodeapps /db/oracle/product ...

  3. 干电池升压5V,功耗10uA

    PW5100干电池升压5V芯片 输出电容: 所以为了减小输出的纹波,需要比较大的输出电容值.但是输出电容过大,就会使得系统的 反应时间过慢,成本也会增加.所以建议使用一个 22uF 的电容,或者两个 ...

  4. 2021年【线上】lammps分子动力学技术实战培训班

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  5. 1、kubernetes简介

    Kubernetes简介 文档信息 中文官网:https://kubernetes.io/zh 中文社区:https://www.kubernetes.org.cn/ Kubernetes是容器集群管 ...

  6. watchdog应用实例

    watchdog应用实例 By 鬼猫猫 20130504 http://www.cnblogs.com/muyr/ 实例:监测某文件夹,一旦文件夹里有文件,就把它剪切到其他服务器 import sys ...

  7. 【图像处理】RGB Bayer Color分析

    Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示,由一半的G,1/4的R,1/4的B组 ...

  8. TCP服务器程序

    Linux下编写TCP服务器调用的函数顺序为:socket -> bind -> listen -> accept -> recv/send socket 参见:http:// ...

  9. CTO也糊涂的常用术语:功能模块、业务架构、用户需求、文档……

    功能模块.业务架构.需求分析.用户需求.系统分析.功能设计.详细设计.文档.业务.技术--很多被随口使用的名词,其实是含糊甚至错误的. 到底含糊在哪里,错误在哪里,不仅仅是新手软件开发人员糊涂,许多入 ...

  10. ADB命令连接逍遥模拟器

    注:打开模拟器开发者模式 ->USB调试模式 1.先进入逍遥模拟器安装目录(MEmu文件夹下),如:D:\Program Files\Microvirt\MEmu 2.在CMD下输入:adb c ...