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 拼接为 SQL 脚本
好像半年前,我就有写过将 Excel 数据, 逐条 或 批量 插入 mysql 数据库, 那时候正在建库嘛, 想着弄个脚本来批量刷新和处理. 工具当时用的 pandas, 这个, 强的一批的工具, 无 ...
- 使用PocketFlowSharp创建一个Human_Evaluation示例
效果 实践 有时候AI生成的结果我们并不满意在进入下一步之前,我们需要对AI生成的结果进行人工审核,同意了才能进入下一个流程. Human_Evaluation就是人工判断的一个简单示例. inter ...
- 在windows server中安装docker
windows server 建议选择win10系统. 首先要下载wsl2: 主要参考微软官方文档:https://learn.microsoft.com/en-us/windows/wsl/inst ...
- B1061 判断题 (15 分)
描述 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人数和判断题数量.第二行给出 ...
- 2025第一届轩辕杯Misc详解
Terminal Hacker 一步到位 flag{Cysay_terminal_game_hacked_successfully} 哇哇哇瓦 foremost分离 GekkoYoru 随波逐流检测, ...
- 用脚手架创建odoo15项目
Odoo 提供了一种机制来帮助建立一个新模块,odoo-bin有一个子命令脚手架来创建一个空模块 命令: $ odoo-bin scaffold <module name> <whe ...
- Spring注解之@Value注解读取配置文件属性和设置默认值
概述 在Spring 组件中,通常使用@Value注解读取 properties 文件的配置值.但如果在配置文件或启动参数中未指定对应的参数值,则项目在启动的时候会抛出异常,导致服务启动失败,异常 ...
- Java 代码块与代码加载顺序
本文首先介绍几个基本的名次,然后介绍了三种代码块的特性和使用方法. 在面试大型公司时,如果遇到大型国企或者大的互联网私企,笔试中经常遇到代码块和代码加载顺序的笔试题.这里做一个总结,也方便各位小伙伴飙 ...
- ActiveMQ的安装与部署
ActiveMQ是Apache的一个开源项目,它是一个功能强劲的开源消息总线,也是一个中间件产品,它是JMS的一个实现. 在介绍ActiveMQ之前,先来复习一下J2EE中的JMS规范.JM ...
- Java HashMap和ConcurrentHashMap知识点梳理
jdk 8 HashMap 扩容之后旧元素存放位置是? java 在扩容的时候会创建一个新的 Node<K,V>[],用于存放扩容之后的值,并将旧的Node数组(其大小记作n)置空:至于旧 ...