首页 > 其他分享 >CF1930D1 - Sum over all Substrings (Easy Version)

CF1930D1 - Sum over all Substrings (Easy Version)

时间:2024-03-22 22:22:52浏览次数:24  
标签:int Sum texttt Substrings Version ans getchar

对于每一个 \(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;
}

标签:int,Sum,texttt,Substrings,Version,ans,getchar
From: https://www.cnblogs.com/yh2021shx/p/18090518

相关文章

  • leetcode 路经总和 pathsum
    很熟悉的一道题,XX二面做过,面试官没让我当场造树,让我用数组模拟树来运行,依旧跑出来了。但是刚刚再做了一下,没思路,不会写......
  • SpringbootLogingApplication has been compiled by a more recent version of the Ja
    一、问题描述:        SpringbootLogingApplicationhasbeencompiledbyamorerecentversionoftheJavaRuntime(classfileversion61.0),thisversionoftheJavaRuntimeonlyrecognizesclassfileversionsupto55.0        更新版本的Ja......
  • This beta version of Typora is expired, please download and install a newer vers
    ThisbetaversionofTyporaisexpired,pleasedownloadandinstallanewerversion.实测最简单有效的方案一、问题突然想看看之前写的笔记,结果typora打不开了,提示ThisbetaversionofTyporaisexpired,pleasedownloadandinstallanewerversion.二、解决方法......
  • Ubuntu上服务运行报错,No usable version of libssl was found
    运行服务时报错sudosystemctlstartComServer.servicesudosystemctlstatusComServer.service×ComServer.service-MESAPIservicesLoaded:loaded(/etc/systemd/system/ComServer.service;enabled;vendorpreset:enabled)Active:failed(Result:cor......
  • System design summary
    systemdesignhttps://github.com/donnemartin/system-design-primerPerformance vs scalabilityscalability这里面的伸缩性是指指标的。当系统有较高的负载时,每个用户仍然能够有较好的响应时,我们说他系统伸缩性强。Performance就是指一个请求的响应越快,自然说性能越高......
  • LeetCode 1161. Maximum Level Sum of a Binary Tree
    原题链接在这里:https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/题目:Giventhe root ofabinarytree,thelevelofitsrootis 1,thelevelofitschildrenis 2,andsoon.Returnthe smallest level x suchthatthesumofa......
  • Atcoder ARC165D Xor Sum 5
    考虑到这个最终的答案是\(\oplus\),所以只需要考虑每种排列出现的次数的奇偶,如果为奇再计算贡献即可。考虑什么情况下出现次数为奇。令每个数出现的个数为\(c_{1\simn}\),方案数即为\(\dbinom{k}{c_1,c_2,\cdots,c_n}=\prod_{i=1}^n\dbinom{k-\sum\limits_{j=1}^{......
  • CodeForces 1943D2 Counting Is Fun (Hard Version)
    洛谷传送门CF传送门被自己的赛时智障操作气笑了。谁告诉你容斥钦定了几个要记到状态里面的。。。/tuu显然先找“好数组”的充要条件。对原数组\(a\)差分,设\(b_i=a_i-a_{i-1}\)。那么一次可以选择一对\((i,j)\)满足\(i\lej-2\),然后给\(b_i\)减\(1\),给\(b_......
  • Arthas - Can not read arthas version from: https://arthas.aliyun.com/api/latest_
    问题描述[ERROR]Cannotreadarthasversionfrom: https://arthas.aliyun.com/api/latest_version[ERROR]CannotfindArthasunderlocal:/root/.arthas/libandremoterepomirror:aliyun[ERROR]Unabletodownloadarthasfromremoteserver,pleasedownload......
  • UVA10829 L-Gap Substrings
    我永远喜欢数据结构。貌似是此题中第一个使用SA+分治+二维数点做法的题解?题目传送门给出字符串\(s\)和常数\(g\),求出有多少四元组\((l_1,r_1,l_2,r_2)\),满足\(s[l_1,r_1]=s[l_2,r_2]\)且\(r_1+g+1=l_2\)。\(T\)组数据,\(1\leT,g\le10\),\(|s|\le5\times10......