CF1930D1 - Sum over all Substrings (Easy Version)
对于每一个 \(f(i, j)\),我们考虑如何计算。我们发现,\(\texttt{1010}\) 式的字符串很有用,所以这启发我们如果遇到了一个模式 \(p_i = \texttt{'1'}\),那么我们可以在 \(i + 1\) 的位置放一个 \(\texttt{'1'}\)。这样我们直接处理了 \(i, i + 1, i + 2\)。容易证明这是最优的。
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
int read() {
char c = getchar();
int x = 0, p = 1;
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') p = -1, c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * p;
}
int n;
string s;
int f(string p) {
int res = 0;
for (int i = 0; i < (int) p.size(); i ++) {
if (p[i] == '1') {
res ++;
i += 2;
}
}
return res;
}
void solve() {
n = read();
cin >> s; long long ans = 0;
for (int i = 0; i < n; i ++) {
string t = "";
for (int j = i; j < n; j ++) {
t += s[j];
ans += f(t);
}
}
cout << ans <<'\n';
}
signed main() {
int t = read();
while (t --) solve();
return 0;
}
CF1930D1 - Sum over all Substrings (Easy Version)的更多相关文章
- 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 ...
- Coffee and Coursework (Easy version)
Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...
- 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION
1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...
- CF1225B1 TV Subscriptions (Easy Version)
CF1225B1 TV Subscriptions (Easy Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...
- Ping-Pong (Easy Version)(DFS)
B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...
- ZOJ 3868 - Earthstone: Easy Version
3868 - Earthstone: Easy Version Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)
Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138 Submit:686 Time Limit: 300 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
随机推荐
- 参考用例之“本地Excel导入系统测试方案”
本地Excel导入系统测试方案 Excel 代码 @Test public void importperson() throws FileNotFoundException { FileInputSt ...
- vue3 学习-初识体验-常见指令v-for和v-model
继续通过小案例来体验一些常用的指令, 以经典的todolist进行展示. 首先呢通过 v-for 指令进行dom循环. v-for 通常是在循环dom的编写的同时遍历数据进行填充. <!DOCT ...
- AutoCAD 逆向工程中 Shx 字体文件解析
数据格式相关的文章 https://wenku.baidu.com/view/8abbfc33eefdc8d376ee32a1.html 代码实现 https://blog.csdn.net/qq_2 ...
- C#中无法将文件”obj\debug\XXX.dll复制到“bin\Debug\XXX.dll” 拒绝访问
较为方便有效的方法就是,把项目属性中的"应用程序集"的"程序集名称"修改为另一个名称即可.
- odoo14里面给所有模型添加方法
给所有的model都添加一个方法, 即所有的model都能调用[类似于create.write.unlink.read]. 方式一: from odoo import api, fields, mod ...
- Spring注解之@Autowired自动装配bean 综述
@Autowired的工作原理是什么?在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowire ...
- 【中英】【吴恩达课后测验】Course 4 -卷积神经网络 - 第四周测验
[中英][吴恩达课后测验]Course 4 -卷积神经网络 - 第四周测验 - 特殊应用:人脸识别和神经风格转换 上一篇:[课程4 - 第三周编程作业]※※※※※ [回到目录]※※※※※下一篇:[待撰 ...
- 网络安全中windows系统常用指令
windows系统常用指令 dir 显示指定目录下的文件和文件夹列表 cd 更改当前工作目录,cd .. 返回上一级目录 cls 清除命令行窗口的内容 echo 在命令提示符(CMD)中输出文本或显示 ...
- 微信小程序中supabase在线数据库使用指南
在微信小程序中使用supabase在线数据库可以无需后端和服务器完成个人小项目的开发.本文记录我的使用过程,在阅读本文前,建议您先满足以下条件: 持有可用于开发的微信小程序 了解kexue上网 第一步 ...
- 利用python、scipy求解差分方程的解
文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html#scipy.signal.lfilt ...