原题链接

题目大意与上题完全一样,只是数据规模更大。

思路:

再用上题的暴力肯定TLE,所以需要优化一下搜索过程。上一题我们是外层遍历n,内层遍历3种情况。这题我们外层遍历3种情况,内层遍历数组,记录每个点的情况,后面的点可以利用前面的结果不用每次重新从零开始计数。同样实时更新最小结果。

代码:

 #include <iostream>
 #include <vector>
 #include <string>
 #define MAX 200005

 using namespace std;

 long long q, n, k;
 string sset = "RGB";

 int main()
 {
     cin >> q;

     while (q--)
     {
         string s;
         cin >> n >> k >> s;
         int ans = MAX;

         ; i < ; i++)
         {
             vector<int> res(n);
             ;
             ; j < n; j++)
             {
                 res[j] = (s[j] != sset[(j+i)%]);
                 cur += res[j];
                 if (j >= k)
                 {
                     cur -= res[j-k];
                 }
                 )
                 {
                     ans = min(ans, cur);
                 }
             }
         }
         cout << ans << endl;
     }
 }

反思:

VJ的C++编译器用的是Microsoft Visual C++ 2010,使用string要加上<string>

---恢复内容结束---

CodeForce-1196D2-RGB Substring (hard version)的更多相关文章

  1. Codeforces 1196D2. RGB Substring (hard version)

    传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...

  2. Codeforces 1196D2 RGB Substring (Hard version) 题解

    题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...

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

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  4. 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 ...

  5. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  6. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”. ...

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

    传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...

  8. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  9. D2. Remove the Substring (hard version)(思维 )

    D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...

  10. D2. Remove the Substring (hard version)

    D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...

随机推荐

  1. 面试题思考:Stack和Heap的区别 栈和堆的区别

    堆栈的概念: 堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常 ...

  2. spring-security问题记录

    一.错误信息 Could not decode JSON for additional information: BaseClientDetails 2019-12-03 22:18:37.239 W ...

  3. 回溯---Permutations II

    47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...

  4. Elasticsearch学习,请先看这一篇!

    原文:Elasticsearch学习,请先看这一篇! 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

  5. EasyUI之DataGrid分页

    第一步创建分页DataGrid <table id="dg"> <thead> <tr> <th data-options="f ...

  6. spark复习笔记(4):RDD变换

    一.RDD变换 1.返回执行新的rdd的指针,在rdd之间创建依赖关系.每个rdd都有一个计算函数和指向父rdd的指针 Spark是惰性的,因此除非调用某个转换或动作,否则不会执行任何操作,否则将触发 ...

  7. Math对象(min()-max()-ceil()-floor()-round()和abs())

    1.Math.min() 功能:求一组数的最小值,返回值:Number 如果里面有非数字则返回NaN 2.Math.max() 功能:求一组数的最大值,返回值:Number 3.Math.ceil() ...

  8. Docker设置容器开机自启动

    设置如下: docker update --restart=always 镜像ID 例如:docker update --restart=always e39a959d7bff. 参考:https:/ ...

  9. oracle 汇编04

    General-Purpose Instructions The general-purpose instructions perform basic data movement, memory ad ...

  10. YOLOV3算法详解

     YOLOV3 YOLO3主要的改进有:调整了网络结构:利用多尺度特征进行对象检测:对象分类用Logistic取代了softmax. 新的网络结构Darknet -53 darknet-53借用了re ...