首页 > 其他 > 详细

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

时间:2020-07-17 22:21:47      阅读:44      评论:0      收藏:0      [点我收藏+]

题目链接:https://codeforces.ml/contest/1196/problem/D2

题意:由一个RGB无限重复构成的字符串, 给定一个字符串s 问最少更换多少个字母使得其中的一个长度为k的子串 是 RGB无限重复的字符串的 子串

思路:因为只有3种可能的开头其他都是重复的, 所以外层枚举 3 种不同的可能, 然后要o (n) 处理出结果, 考虑 处理的是子串 是一段连续的区间

要o (n) 处理的话不难想到用前缀和维护,   值得注意的就是三种枚举的时候怎么才最简便

技术分享图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 const int maxn =2e5+10;
 6 #define pb push_back
 7 char t[10]={R,G,B};
 8 char s[maxn];
 9 int pre[maxn];
10 
11 int main()
12 {
13     ios::sync_with_stdio(false);
14     cin.tie(0);
15     int q;
16     cin>>q;
17     while(q--)
18     {
19         int n,k;
20         cin>>n>>k;
21         cin>>(s+1);
22         int ans=1e9;
23         for(int i=0;i<3;i++)
24         {
25             for(int j=1;j<=n;j++)
26             {
27                 if(s[j]!=t[(i+j)%3])
28                     pre[j]=1;
29                 else
30                     pre[j]=0;
31             }
32             for(int j=1;j<=n;j++)
33             {
34                 pre[j]+=pre[j-1];
35             }
36             for(int j=k;j<=n;j++)
37             {
38                 ans=min(ans,pre[j]-pre[j-k]);
39             }
40         }
41 
42         cout<<ans<<\n;
43     }
44 
45 
46 
47 
48 
49 
50 }
View Code

 

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

原文:https://www.cnblogs.com/winfor/p/13332600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!