传送门

题意:

给你一个长为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. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT 故障排除

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  2. rm: cannot remove `/tmp/localhost-mysql_cacti_stats.txt': Operation not permitted

    [root@DBslave tmp]# chown zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt

  3. Trollcave-suid提权

    一 扫描端口 扫描开放端口:nmap -sV -sC -p- 192.168.0.149 -oA trollcave-allports 扫描敏感目录:gobuster dir -u http://19 ...

  4. SAPCAR使用说明

    1.首先看一下SAPCAR的功能usage:create a new archive:SAPCAR -c[vir][f archive] [-P] [-C directory]   [-A filen ...

  5. webapi Swagger 配置 services.BuildServiceProvider() 报警 ASP0000 问题处理

    问题起源 网上的常见配置 Swagger 配置 在Startup类的 ConfigureServices 使用 services.BuildServiceProvider() ,其中有段代码如下: v ...

  6. 数据分析——Numpy/pandas

    NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准数学函数(无需编 ...

  7. Scrapy———反爬蟲的一些基本應對方法

    1. IP地址驗證 背景:有些網站會使用IP地址驗證進行反爬蟲處理,檢查客戶端的IP地址,若同一個IP地址頻繁訪問,則會判斷該客戶端是爬蟲程序. 解決方案: 1. 讓Scrapy不斷隨機更換代理服務器 ...

  8. jQuery 点击input框标题收起

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. git的使用学习笔记---合并分支

    一.使用场景 不同的分支需要合并 二.操作 1.首先要创建一个分支 git checkout -b mergedemo 创建文本 vim text.txt 添加文本 git add text.txt ...

  10. UVA11694 Gokigen Naname题解

    目录 写在前面 Solution Code 写在前面 UVA的题需要自己读入一个 \(T\) 组数据,别被样例给迷惑了 Solution 每个格子只有两种填法且 \(n \le 7\),暴力搜索两种填 ...