CF 1196D2 RGB Substring (hard version) --- 前缀和 + 思维

简单的情况不用前缀和,直接暴力就好,数据范围大的D2,就用前缀和数组存不满足三种情况的数,最后减一下取小就可以。

 1 #include<bits/stdc++.h>
 2 #define mem(a) memset(a,0,sizeof(a))
 3 #define ll long long
 4 #define ld long double
 5 #define ull unsigned long long
 6 #define mp make_pair
 7 #define pb push_back
 8 #define inf 0x3f3f3f3f
 9 using namespace std;
10 const int N=2e5+5;
11 const int M=100+5;
12 int n,t,k,x[N],y[N],z[N];
13 string s;
14 char ss[3]={‘R‘,‘G‘,‘B‘};
15 int main()
16 {
17     cin>>t;
18     while(t--)
19     {
20         cin>>n>>k;
21         cin>>s;
22         int ans=n+5;
23         x[0]=y[0]=z[0]=0;
24         for(int i=0;i<n;i++)
25         {
26             if(s[i]!=ss[i%3]) x[i+1]=x[i]+1;
27             else x[i+1]=x[i];
28             if(s[i]!=ss[(i+1)%3]) y[i+1]=y[i]+1;
29             else y[i+1]=y[i];
30             if(s[i]!=ss[(i+2)%3]) z[i+1]=z[i]+1;
31             else z[i+1]=z[i];
32         }
33         for(int i=0;i+k<=n;i++)
34         {
35             ans=min(min(ans,x[i+k]-x[i]),min(y[i+k]-y[i],z[i+k]-z[i]));
36         }
37         cout<<ans<<endl;
38     }
39 }

(? •_•)?

原文地址:https://www.cnblogs.com/XXrll/p/11264399.html

时间: 2024-08-22 06:59:22

CF 1196D2 RGB Substring (hard version) --- 前缀和 + 思维的相关文章

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 #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) 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) D2. RGB Substring (hard version)

传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在“RGBRGBRGBRGB........(以RGB为循环节,我们称这个串为str)”里面也是一个子串,这个子串的长度是k 可是有可能s字符串中找不到,那么这个时候就可以改变s字符串中某些位置的字母来完成任务.问最少需要改变多少个字母 题解: 主要看暴力的姿势对不对.在上一道的D1上面,我是对s字符串的每一个位置进行‘R’,‘G’,‘B’的枚举,因为如果这个子串也是str的子串的话

Codeforces 873 B. Balanced Substring(前缀和 思维)

题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. 1 #include<bi

CF 479E Riding in a Lift 前缀和 DP

输入 n a b k 有n层楼 起点在a层 b层是不能到达的 假设当前在x层 每一次可以到达y层 满足 |x-y| < |x-b| 求进行k次的不同方案数 dp[i][j]为第i次到达j层的方案数 dp[i][j] = sum(dp[i-1][k])  其中|k-j| < |k-b| 满足条件的k是连续的一段 用前缀和优化 #include <cstdio> #include <cstring> #include <cstdlib> using namesp

Codeforces - 1203D2 - Remove the Substring (hard version) - 双指针

https://codeforces.com/contest/1203/problem/D2 上次学了双指针求两个字符串之间的是否t是s的子序列.但其实这个双指针可以求出的是s的前i个位置中匹配t的最长的前缀.反过来求一次可以得到最长的后缀. 然后怎么找要删除的位置呢?暴力n^2肯定可以,然后线性写挂到自闭. 枚举位置[i,j),注意j可以取相等,所以预处理前后缀的时候把n位置的后缀也算好. 去除子串[i,j),那么剩下的就是[0,i-1]和[j,n-1]两个子串,他们匹配的长度加起来超过tl就

题解 CF1203D2 【Remove the Substring (hard version)】

题目链接 Solution CF1203D 题目大意:给定两个字符串\(s\),\(t\), \(t\)是\(s\)的子序列,问最长可以在\(s\)里面删掉多长的连续子序列使得\(t\)仍为\(s\)的子序列 贪心 分析: 假如我们\(t\)的一个前缀要匹配\(s\)的话,显然尽可能往前匹配,这样可以使得答案尽量大,后缀同理 我们用\(suf[i]\)表示\(t[1\dots i]\)可以匹配\(s\)前缀的最前的位置,\(suf[i]\)表示\(t[i \dots |t|]\)可以匹配\(s\

Codeforces 1203D2 Remove the Substring (hard version)

题目链接:https://www.luogu.org/problem/CF1203D2 题意:给你两个字符串s,t(长度为2e5),保证t是s的子序列,求问最大能在s中删子串的长度,且保证删后t还是s的子序列 分析:先求pre和last两个数组,分别保存最左边的满足t的子序列后最右边的满足t的子序列. 之后依次比较即可 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3