对于每一个 \(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)的更多相关文章

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

  2. Coffee and Coursework (Easy version)

    Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...

  3. 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION

    1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...

  4. CF1225B1 TV Subscriptions (Easy Version)

    CF1225B1 TV Subscriptions (Easy Version) 洛谷评测传送门 题目描述 The only difference between easy and hard vers ...

  5. Ping-Pong (Easy Version)(DFS)

    B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. ZOJ 3868 - Earthstone: Easy Version

    3868 - Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld ...

  7. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

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

  9. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

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

随机推荐

  1. 操作系统综合题之“短进程优先调度算法(Shortest-Process-First,SPF)和非抢占式优先权调度算法(Priority-Scheduling Lgorithm)的进程执行顺序并计算周转时间以及平均周转时间【分开计算题】”

    一.问题: 1.当系统采用短进程优先调度算法时,试写出进程的执行顺序,并计算各个进程的周转时间以及平均周转时间 2.当系统采用优先权调度算法时,试写出进程的执行顺序,并计算各个进程的周转时间以及平均周 ...

  2. .NET外挂系列:2. 了解强大的 harmony 注解特性

    一:背景 1. 讲故事 上一篇我们简单的聊了下harmony外挂的基本玩法,让大家宏观上感受到了外挂在 .NET高级调试 领域的威力,这一篇我们从 注解特性 这个角度继续展开. 二:harmony 注 ...

  3. CentOS、Ubuntu安装jdk11方法

    CentOS: sudo yum install java-11-openjdk -y Ubuntu sudo apt-get install openjdk-11-jre -y 检查版本: java ...

  4. Win32汇编学习笔记04.重定位与汇编引擎

    Win32汇编学习笔记04.重定位与汇编引擎-C/C++基础-断点社区-专业的老牌游戏安全技术交流社区 - BpSend.net 重定位 **重定位:**也称为代码自重定位,代码自己去计算自己使用的各 ...

  5. Qt图像处理技术七:轮廓提取

    Qt图像处理技术七:轮廓提取 效果图 原理 图像先二值化让rgb数值相同,只有(0,0,0)或者(255,255,255) 取每个点的周围8个点,如果周围8个点与该点rgb值相同,则需要将该点描黑为( ...

  6. PLsql and 汉化包的安装使用

    一.准备工作 1.点击下载PLSQL,本次安装的PLSQL版本为12.0.7,建议安装64位. 2.下载PLSQL时,版本旁边会有个"Language pack"的链接,点击后左侧 ...

  7. JAVA经典算法分析

      算法分析是对一个算法需要多少计算时间和存储空间作定量的分析. 算法(Algorithm)是解题的步骤,可以把算法定义成解一确定类问题的任意一种特殊的方法.在计算机科学中,算法要用计算机算法语言描述 ...

  8. AES256加密解密

    REPORT zged_aes. DATA lv_message_string TYPE string. DATA lv_message_decrypted TYPE XSTRING. " ...

  9. Java团队Cursor最佳实践:3分钟构建「零泄漏」AI开发环境

    最近用了段时间的 cursor,  发现卷程序员的还是程序员自己,  开发效率确实飞起了. 没办法, 开源卷自己.AI还是第一个卷自己, 但对于我们个人来说, 只能拥抱变化. 本文总结了下最近用到的基 ...

  10. 为什么 `kubectl patch` 关闭探针不重启 Pod,重新开启却重启?

    揭秘 Kubernetes 探针机制与 Pod 不可变性的博弈 在 Kubernetes 运维中,一个常见现象引发困惑:关闭探针(如 LivenessProbe)时 Pod 不会重启,但重新启用后却可 ...