传送门

题意:

给你一个长为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. XSS-labs通关挑战(xss challenge)

    XSS-labs通关挑战(xss challenge) 0x00 xss-labs   最近在看xss,今天也就来做一下xss-labs通过挑战.找了好久的源码,终于被我给找到了,因为在GitHub上 ...

  2. 如何利用Intellij Idea搭建python编译运行环境 (转)

    首先进入Intellij Idea的官方网站:点击打开链接 点击download,选择旗舰版进行下载.网上的破解教程很多,也可以注册一个学生账号拿到一年的免费试用权. 安装过程不再细说,第一次打开选择 ...

  3. kubernets之pod的标签拓展

    一 标签的拓展使用 1.1 标签的作用范围不仅仅适用于pod对node以及其他类的大部分资源同样适用 k label node node01 gpu=true k是kubectl的别名形式 同样对于n ...

  4. python无法连接mysql,有个小坑,需要注意一下!

    尝试用python链接mysql数据库,按照教程里链接了小半夜,就是没有链接上,找了各种文章,最后发现是版本的问题,我想使用的模块是MySQLdb,刚刚去他们的官网看了一下,最近一次更新是2014年, ...

  5. ALV中layout布局控制详解

    参数的结构为SLIS_LAYOUT_ALV.结构中比较常用的字段如下: no_colhead      隐藏列标题          值为X或空 no_hotspot     headings不作为热 ...

  6. Redis-第五章节-8种数据类型

    目录 一.Redis对key的操作 二.五种数据类型 String类型 List(集合) Set(集合) Hash(哈希) Zset(有序集合) 三.三种特殊数据类型 geospatial(地理位置) ...

  7. 欢迎来到 ZooKeeper 动物世界

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费有趣.入门级的 ZooKeeper 开源教程,面向有编程基础的新手. Zo ...

  8. Python+Selenium+Unittest实现PO模式web自动化框架(6)

    1.TestCases目录下的模块 TestCases目录下是存放测试用例的目录. TestCases目录下的测试用例采用unittest框架来构建. 例如:登录功能的测试用例.(test_1_log ...

  9. RPC 接口必须是业务职责

    https://mp.weixin.qq.com/s/MYSF8lCF92ItG_Lc8nOspg 一个加班多新人多团队,我们的代码问题与重构 陈于喆 高可用架构 2020-10-21   微服务编码 ...

  10. (Oracle)数据量统计存储过程

    本过程适用于Oracle数据量统计. create or replace procedure SP_GET_TAB_COUNT as v_tableName HDSD_TJ.Tablename%typ ...