传送门

题意:

给你一个长为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. 深入理解static、volatile关键字

    static 意思是静态的,全局的.被修饰的东西在一定范围内是共享的,被类的所有实例共享,这时候需要注意并发读写的问题. 只要这个类被加载,Java虚拟机就能根据类名在运行时数据区的方法区内找到他们. ...

  2. python模块详解 | selenium(持续更新中)

    目录: 关于selenium Selenium 安装Selenium 安装浏览器驱动 配置环境变量 selenium方法详解 定位元素 元素操作 浏览器操作 鼠标事件 浏览器事件 设置元素等待 多表单 ...

  3. 【Spring】 Spring的核心容器

    Spring的核心容器 文章目录 Spring的核心容器 BeanFactory ApplicationContext 1.通过ClassPathXmlApplicationContext创建 2.通 ...

  4. 【ASM】asm中添加 diskgroup

    环境:rhel5 Oracle10g rac 背景:在esxi中添加了一个20g的共享磁盘准备存放归档日志用 一.准备环境 1.添加共享磁盘并且格式化 #fdisk -l查看磁盘已经添加完成 #fdi ...

  5. Python利用最优化算法求解投资内部收益率IRR【一】

    一. 内部收益率和净现值 内部收益率(Internal Rate of Return, IRR)其实要和净现值(Net Present Value, NPV)结合起来讲.净现值指的是某个投资项目给公司 ...

  6. USB过压保护芯片,高输入电压充电器(OVP)

    PW2606B是一种前端过电压和过电流保护装置.它实现了广泛的输入电压范围从2.5VDC到40VDC.过电压阈值可在外部或外部编程设置为内部默认设置.集成功率路径nFET开关的低电阻确保了更好的性能电 ...

  7. JD6621快速充电协议芯片,带有PPS 控制器的USB-PD3.0

    描述 JD6621是高度集成的USB供电(PD)控制器,支持USB PD 3.0 ,该USB PD 3.0 具有针对USBType-C下游接口(源)设计的可编程电源(PPS)规范.它监视CC引脚以检测 ...

  8. 使用Spring的RestTemplate进行接口调用

    引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...

  9. How to Gracefully Close Channels

    小结: 1. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership ...

  10. Fody,告别烦人的INotifyPropertyChanged,最简方式实现通知!

    INotifyPropertyChanged 我不是针对谁,我是说在座的各位 相信所有学wpf的,都写过类似下面的代码: 实现INotifyPropertyChanged public class M ...