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

传送门

题意:

给你一个长为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 }

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11529817.html

时间: 2024-07-30 14:02:15

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

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

D2. RGB Substring (hard version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a string ss cons

Codeforces Round #575 (Div. 3)记录

Codeforces Round #575 (Div. 3)记录 错过了上分的机会,上次不小心打了个div. 2结果直接掉了100多分. 我绿了,也变弱了.找下场Div. 3上上分吧. A 随便写了. 我的思路是三个东西先排序,一个人先拿最少的,另一个人拿次少的. 然后看剩下的能不能填补相差,如果能的话继续左右两边各补,补到剩1或0为止. 其实上面说这么多,答案就等于\(\lfloor \frac{a+b+c}{2} \rfloor\). 我是sb B 这些数与大小无关,我们直接统计有多少个奇数

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

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

D2. RGB Substring (hard version)||D1. RGB Substring (easy version)

D2. RGB Substring (hard version)    原题传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a strin

Codeforces Round #350 (Div. 2) D2 二分

五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多少个 每次对mid求一下不够的差值 比较差值与m的大小进行l与r的变换 由于自己一向对二分比较迷茫 自己琢磨出来一套神奇的办法面对边界数据 当小于和大于的时候 抛弃mid值 当等于的时候 直接break 然后打一发while试试能否向更好的情况偏移 当然在这个题目中 如果是直接break的时候就不用

Codeforces Round #527 (Div. 3)D2(栈,思维)

#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int n;    int mn=0;    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(a[i]>mn)            mn=a

Codeforces Round #575 (Div. 3)

本蒟蒻已经掉到灰名了(菜到落泪),希望这次打完能重回绿名吧...... 这次赛中A了三题 下面是本蒟蒻的题解 A.Three Piles of Candies 这题没啥好说的,相加除2就完事了 #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll q; scanf("%lld",&q); while(q--) { ll a,b,c; scanf("

Codeforces Round #575 (Div. 3) C. Robot Breakout (模拟,实现)

C. Robot Breakout time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output n robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental,