CodeForce-1196D2-RGB Substring (hard version)
题目大意与上题完全一样,只是数据规模更大。
思路:
再用上题的暴力肯定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)的更多相关文章
- Codeforces 1196D2. RGB Substring (hard version)
传送门 考虑枚举每一个位置作为可能子段的起点,然后对以这个位置为起点的所有情况下的答案取 $min$ 当固定了起点 $i$ 并且固定了起点 $i$ 最终的字符时,答案也固定了 发现对于所有与 $i \ ...
- Codeforces 1196D2 RGB Substring (Hard version) 题解
题面 \(q\) 个询问,每个询问给出一个字符串 \(s\),要你在 \(s\) 中用最小替换得到无穷字符串 RGBRGBRGB... 的长度为定值 \(k\) 的子串. 题解 一眼看过去可能是编辑距 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- 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 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 “R”. ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)
传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在"RGBRGBRGBRGB........(以RGB为循环节,我们称这个串 ...
- 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 ...
- D2. Remove the Substring (hard version)(思维 )
D2. Remove the Substring (hard version) time limit per test 2 seconds memory limit per test 256 mega ...
- D2. Remove the Substring (hard version)
D2. Remove the Substring (hard version) 给字符串s,t,保证t为s的子序列,求s删掉最长多长的子串,满足t仍为s的子序列 记录t中每个字母在s中出现的最右的位置 ...
随机推荐
- 第二次java面试(用友山东济南分公司)
坐标:山东潍坊公共实训基地 面试单位:用友济南分公司(来了一位HR和技术经理) 本人状态:距离离校15天 宣讲: 1.女HR和男技术经理来到我们专业提前准备好的教室,先宣传海报和发传单,然后看了4个3 ...
- 牛客小白月赛16 A 小石的签到题 ( 博弈)
链接:https://ac.nowcoder.com/acm/contest/949/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- ARM汇编3
一. 什么是协处理器? 1.1. SoC内部另一处理核心,协助主CPU实现某些功能,被主CPU调用执行一定任务. 1.2. ARM设计上支持多达16个协处理器,但是一般SoC只实现其中的CP15.(c ...
- 【Matlab技巧】工作区变量如何添加到Simulink中?
对新手来说,在进行simulink仿真时想把工作区的变量添加到Simulink中,这样在如transfer模块中使用时可以直接输变量即可. 如这样: 那么如何对Simulink仿真文件自动赋值呢? 1 ...
- GitHub上更新原有的项目代码(二)
转载自:https://blog.csdn.net/dayewandou/article/details/79175783 项目上传了,现在又写了一些内容想要跟新到项目上去,怎么更新呢? 首先进入项目 ...
- MySQL第五天——日志
日志 log_error(错误日志) 用于记录 MySQL 运行过程中的错误信息,如,无法加载 MySQL数据库的数据文件,或权限不正确等都会被记录在此. 默认情况下,错误日志是开启的,且无法禁止. ...
- linux 配置 Sersync
[root@SERSYNC sersync]# cp conf/confxml.xml conf/confxml.xml.bak.$(date +%F) [root@SERSYNC sersync]# ...
- 日志远程更新脚本shell
log_update.sh.bak: #!/bin/bash# To Update Logs... #/bin/rsync -avz --delete --exclude=warn.log @172. ...
- (转)nginx+redis实现接入层高性能缓存技术
转自:https://blog.csdn.net/phil_code/article/details/79154271 1. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性 ...
- bzoj5084 hashit 广义SAM+树链的并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5084 题解 考虑平常对于静态问题,我们应该如何用 SAM 求本质不同的子串个数. 对于一个常规 ...