传送门

题意:

给你一个长为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. Flutter 基础组件:状态管理

    前言 一个永恒的主题,"状态(State)管理",无论是在React/Vue(两者都是支持响应式编程的Web开发框架)还是Flutter中,他们讨论的问题和解决的思想都是一致的. ...

  2. Azure 存储简介

    Azure Storage Account(存储账户)包含所有Azure Storage的数据对象,包括Blob.Data Lake Gen2,File.Queue.Disk和Table等服务,该St ...

  3. Ubuntu 18.04.4 LTS 更换国内系统源

    Ubuntu 18.04.4 LTS 更换国内系统源 1.1) 好习惯先做备份在干活: mv /etc/apt/sources.list /etc/apt/sources.list.bak 1.2) ...

  4. 【Java】一个简单的Java应用程序

    简单记录,Java 核心技术卷I 基础知识(原书第10 版) 一个简单的Java应用程序"Hello, World!" Hello, World! Goodbye,World! 一 ...

  5. xtrabackup_binlog_info

    文件保存了备份结束时刻binlog的名称和位置

  6. springBoot实现redis分布式锁

    参考:https://blog.csdn.net/weixin_44634197/article/details/108308395 .. 使用redis的set命令带NX(not exist)参数实 ...

  7. linux自定义位置安装tomcat8.5

    1 下载tomcat安装文件 下载地址:https://tomcat.apache.org/download-80.cgi  2 解压文件 tar -zxvf apache-tomcat-8.5.56 ...

  8. Matlab GUI学习总结

    从简单的例子说起吧.   创建Matlab GUI界面通常有两种方式:   1,使用 .m 文件直接动态添加控件     2.  使用 GUIDE 快速的生成GUI界面显然第二种可视化编辑方法算更适合 ...

  9. MySQL之谓词下推

    MySQL之谓词下推 什么是谓词 在SQL中,谓词就是返回boolean值即true或者false的函数,或是隐式转换为boolean的函数.SQL中的谓词主要有 LKIE.BETWEEN.IS NU ...

  10. How to kill go routine?

    How to kill go routine? https://stackoverflow.com/questions/37997608/kill-a-method-in-an-infinite-lo ...