AtCoder - 4496 G - k-DMC
AtCoder - 4496 G - k-DMC
题目
长度为n的字符串,q次查询,问“DMC”(不要求连续)在字符串中出现的次数,其中D和M的距离不超过k。
错误思路
通过遍历字符串中的每一个“M”,再移动窗口,处理左右“D”、“C”的数量。(TLE)
题解
滑动窗口,维护当前窗口中"D"、“M”、“DM”的数量,遇“C”则ans加上"DM"的数量。
代码
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef long long ll;
int main(){
int n,q;
string s;
cin>>n>>s>>q;
while (q--) {
int k;cin>>k;
ll d=0,m=0,dm=0,dmc=0,i;
for(i=0;i<k;i++){
switch (s[i]) {
case 'D':
d++;
break;
case 'M':
dm+=d;
m++;
break;
case 'C':
dmc+=dm;
}
}
for(;i<n;i++){
switch (s[i-k]) {
case 'D':
d--;
dm-=m;
break;
case 'M':
m--;
}
switch (s[i]) {
case 'D':
d++;
break;
case 'M':
dm+=d;
m++;
break;
case 'C':
dmc+=dm;
}
}
printf("%lld\n",dmc);
}
}
AtCoder - 4496 G - k-DMC的更多相关文章
- 二分箭术--G&K稳健过神思路
自从资料片开始,一直在思考稳定可靠的过神思路,现在有眉目了,试验了几把感觉不错,先分享如下: 1)只开2个分城,特殊情况除外.Re: 经过多次打的经验,开2个分城比开3个分城更容易选址,政策更快,快乐 ...
- Atcoder Regular-074 Writeup
C - Chocolate Bar 题面 There is a bar of chocolate with a height of H blocks and a width of W blocks. ...
- 【AtCoder】AtCoder Petrozavodsk Contest 001
A - Two Integers 如果\(X\)是\(Y\)的倍数的话不存在 可以输出\(X \cdot (\frac{Y}{gcd(X,Y)} - 1)\) 代码 #include <bits ...
- AtCoder Beginner Contest 113 C
C - ID Time limit : 2sec / Memory limit : 1024MB Score: 300 points Problem Statement In Republic of ...
- Atcoder E - RGB Sequence(dp)
题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...
- AtCoder,Codeforces做题记录
AGC024(5.20) 总结:猜结论,“可行即最优” B: 给定一个n的排列,每次可以将一个数移到开头或结尾,求变成1,2,...,n所需的最小步数. 找到一个最长的i,i+1,...,j满足在排列 ...
- 数组第K小数问题 及其对于 快排和堆排 的相关优化比较
题目描述 给定一个整数数组a[0,...,n-1],求数组中第k小数 输入描述 首先输入数组长度n和k,其中1<=n<=5000, 1<=k<=n 然后输出n个整形元素,每个数 ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- K - Treasure Exploration - POJ 2594(最小路径覆盖+闭包传递)
题意:给一个有向无环图,求出来最小路径覆盖,注意一个点可能会被多条路径重复 分析:因为有可能多条路径走一个点,可又能会造成匹配的不完全,所以先进行一次闭包传递(floyd),然后再用二分匹配的方法求出 ...
随机推荐
- CF1136E Nastya Hasn't Written a Legend(线段树)
还能说什么呢,简直太妙了. $$a_{i+1}<a_i+k_i$$ $$a_{i+1}-k_i-k_{i-1}-\cdots-k_1<a_i+k_i-k_i-k_{i-1}-\cdots- ...
- [LeetCode] 913. Cat and Mouse 猫和老鼠
A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The grap ...
- [LeetCode] 890. Find and Replace Pattern 查找和替换模式
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- [LeetCode] 277. Find the Celebrity 寻找名人
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- 数据库多行数据合并一行(sqlserver、Oracle、Mysql)
我们日常查询数据时,经常会有将查询到的数据按照某一列分组显示(合并多行数据),比如: 表结构: ),coursename )); 需要将以上数据按照用户名分组,所选课程列不同项之间用逗号隔开,在一行中 ...
- [Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目
获取要发布的定时计划任务. 禁用和停止定时计划任务. 批量强制结束Job进程. 打印定时计划任务状态. 备份项目文件夹. 发布项目文件夹. 删除部署包. 启用定时计划任务. <# .NOTES ...
- express常见获取参数的方法
1.req.query 处理get请求 // GET /search?q=tobi+ferret req.query.q // => "tobi ferret" // GET ...
- 2017 ACM/ICPC Asia Regional Shenyang Online E number number number 题解
分析: 当n=1时ans=4=f(5)-1; n=2,ans=12=f(7)-1; n=3,ans=33=f(9)-1; 于是大胆猜想ans=f(2*k+3)-1. 之后用矩阵快速幂求解f(n)即可, ...
- 另一种缓存,Spring Boot 整合 Ehcache
用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...