Codeforces 165C Another Problem On Strings 尺取 Or 前缀和

从Si3N4到SiNx:一文搞懂芯片制造中氮化硅的化学式玄机(附工艺参数影响) 本文深入探讨了芯片制造中氮化硅(SiNx)化学式的调控艺术,揭示了从Si3N4到SiNx的转变如何影响材料性能。通过分析PECVD和PVD工艺参数对x值的精确调控,以及x值对电学和机械性能的影响,为半导体制造提供了实用的工艺优化策略。 阅读详情

点击打开链接

题意:给出串s,长度<=1e6 求出正好包含k个1的子串个数?(字串位置不同就算不同)


尺取:以l为左端点成立的子串,若第一次在r成立 最后一次在y成立(r后的第一个1在y+1) 则个数为y-r+1

第一次在r成立,[l,r]成立 [l+1,r-1]肯定不成立  dp记录i后的第一个1 复杂都为O(2*n)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+20;
char s[N];
int dp[N];
int main()
{
	int k;
	while(cin>>k)
	{
		scanf("%s",s+1);
		int n=strlen(s+1);
		ll ans=0;
		//O(N)
		if(k==0)
		{
			int i=1;
			while(s[i])
			{
				int r=i;
				while(s[r]=='0')
					r++;
				if(r>i)
				{
					r--;
					ll x=r-i+1;
					ll y=(1+x)*x/2;
					ans+=y;
					i=r+1;
				}
				else
				i++;	
			}
			cout<<ans<<endl;
			continue;
		}
		dp[n+1]=n+1;
		for(int i=n;i>=1;i--)
			dp[i]=s[i]=='1'?i:dp[i+1];

		//尺取:以i为左端点成立的子串
		ll l=1,r=1,cnt=0,y; 
		while(s[l])
		{
			while(s[r]&&cnt<k)
			{
				if(s[r]=='1')
				{
					cnt++;
					if(cnt==k)
					break;
				}
				r++;		
			}
			if(cnt<k)
			break;//没有k个1
		
			//[l,r]成立 [l,y]?   
			//dp优化预处理出,点r+1后第一个'1'的出现位置-r
			ans+=dp[r+1]-r;	
	
			if(s[l]=='1')
			{
				cnt--;
				r++;
			}
			l++;		
		}
		cout<<ans<<endl;
	}
	return 0;
}


用前缀和思想更为简单
由题意子串1个数为k,则子串和应该为k,cnt[i]记录和为i的前缀个数,容易得到以i结尾的字串和为k的个数为:cnt[p[i]-k]]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+20;
char s[N];
ll cnt[N],p[N];
//cnt[i]:前缀和为i个数 
//p[N]:1~i的前缀和 
int main()
{
	int k;
	while(cin>>k)
	{
		memset(cnt,0,sizeof(cnt));
		ll ans=0;
		scanf("%s",s+1);
		p[0]=0;
		cnt[0]=1;//
		//计算以i结尾 字串和为k个数 
		for(int i=1;s[i];i++)
		{
			p[i]=p[i-1];
			if(s[i]=='1')
			p[i]++;
			if(p[i]>=k)
			ans+=cnt[p[i]-k]; 
			cnt[p[i]]++;
		}
		cout<<ans<<endl;
	}
	return 0;
}




4.26华为OD机试真题 新系统 - 端口流量统计 (Java/Py/C/C++/Js/Go) 本文整理华为 OD 机试新系统机考真题库,涵盖高频考点精析与完整题目内容,提供 C 语言、Java、C++、Python、Go、JavaScript 多语言源码实现,搭配详细解题思路讲解,并支持在线 OJ 刷题练习,助力高效备考华为 OD 机试新系统。 阅读详情

相关推荐

四旋翼无人机控制算法实战:从PID到MPC的路径跟踪

本文深入探讨了四旋翼无人机控制算法的演进路径,从基础的PID控制到高级的MPC算法,详细解析了各类算法的原理、实现技巧及适用场景。通过实战案例对比分析,为开发者提供从算法选型到参数调优的全方位指导,助力提升无人机飞行控制的精度与稳定性。

weixin_29090917的博客 245

cf165c Another Problem on Strings

Another Problem on Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A string is binary, if it consis

Clearlemon 929

PredFormer实战:用Transformer搞定交通流量预测(附TaxiBJ数据集代码)

本文详细介绍了如何利用PredFormer这一基于Transformer的先进模型进行交通流量预测,特别针对TaxiBJ数据集提供了完整的实战代码和优化策略。PredFormer通过门控Transformer块(GTB)的创新设计,在时空预测任务中展现出显著优势,相比传统方法将MSE降低33.1%,推理速度提升4.4倍。文章还涵盖了模型轻量化、部署优化等实用技巧,为智慧城市建设中的交通预测提供了高效解决方案。

weixin_29194817的博客 450

Codeforces 165C Another Problem on Strings 【二分】

题目链接:Codeforces 165C Another Problem on Strings C. Another Problem on Strings time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A string i

世界很大 1301

硬件工程师必看:5种ESD保护电路设计实战解析(附电路图)

本文为硬件工程师深度解析5种实战ESD保护电路设计,涵盖TVS二极管、专用保护芯片、片上防护及高速射频场景方案。通过具体电路图,详解工作原理、选型要点及PCB布局黄金法则,重点强调ESD防护设计中的能量泄放路径管理与低阻抗实现,助力提升产品可靠性。

xk7n92h3p5m的博客 658

告别PlatformIO!用Arduino IDE搞定ESP32上的micro-ROS 2.0.5(Humble版)

本文详细介绍了如何从PlatformIO迁移到Arduino IDE以支持ESP32上的micro-ROS 2.0.5(Humble版)。内容涵盖环境配置、项目转换、常见问题解决及高级优化技巧,帮助开发者顺利过渡并充分利用Arduino IDE的官方支持优势。

weixin_33709590的博客 381

Codeforces 165C Another Problem on Strings(尺取法)

题目 题目链接Problem - 165C - Codeforces C. Another Problem on Strings 2 seconds 256 megabytes A string isbinary, if it consists only of characters "0" and "1". Stringvis asubstringof stringwif it has a non-zero length and can be read sta...

Carry_hkr的博客 368

Arduino Uno 实验15——MQ-135 气体传感器模块

MQ-135 气体传感器模块简介(空气质量检测 有害气体检测)   MQ135气体传感器所使用的气敏材料是在清洁空气中电导率较低的二氧化锡(SnO2)。当传感器所处环境中存在污染气体时,传感器的电导率随空气中污染气体浓度的增加而增大。MQ135气体传感器对氨气、硫化物、苯系蒸汽的灵敏度高,对烟雾和其它有害气体的监测也很理想。这种传感器可检测多种有害气体,是一款适合多种应用的低成本传感器。   主要用途:家庭和环境的有害气体检测装置   主要芯片:LM393、MQ-135气体传感器 不同气体的型号选择

weixin_42645653的博客 2万+

CodeForces 165C - Another Problem on Strings

C. Another Problem on Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A string is binary, if it con

coco430的专栏 1270

在 Android Studio 中接入 DeepSeek:加速 Android 开发进程

通过在 Android Studio 中接入 DeepSeek,Android 开发者能够充分利用人工智能技术带来的强大优势,显著提升开发效率和代码质量。从前期的安装准备,到具体的接入步骤,再到使用过程中的技巧和注意事项,每一个环节都为打造高效、智能的开发环境提供了有力支持。希望本文能帮助你顺利将 DeepSeek 融入到 Android Studio 的开发工作流程中,让你在 Android 开发的道路上更加得心应手,创造出更优质的 Android 应用。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 4180

2019年高教社杯全国大学生数学建模竞赛 C 题:《机场的出租车问题》真题解析与 MATLAB 解决方案

🏆 本文已收录于专栏:《滚雪球学数学建模(含历年真题)》,持续更新最新及历年数学建模竞赛资料,每篇内容力求做到“问题讲透、模型讲清、代码可用、论文可参考”,帮助你从零开始逐步建立完整的数学建模知识体系。

**My Coding Family** 3474

CodeForces - 165C Another Problem on Strings 前缀和+二分

题意: 给定一个01串和k,求包含k个1的子串的个数 思路: 用前缀和记录到当前位置的个数,那么就可以知道某个子串的1的个数 我们遍历字符串 对于每个位置i,以他为开头的子串,要求1的个数为k的数量,我们只需要求有多少个值等于sum[i-1]+k就可以了, 枚举肯定不行,因为是前缀和有序,我们二分,两次二分 upper_bound(sum+1,sum+len+1+1,sum[i-1]+k)-low...

青春苦瓜 325

ROS & ROS2 机器人深度相机激光雷达多传感器标定工具箱

不过,多传感器标定工具箱允许通过编辑 yaml 文件来调整标定目标的参数: <repository>/mutlisensor_calibration/cfg/TargetWithCirclesAndAruco.yaml%YAML:1.0rows: 4cols: 1dt: irows: 4cols: 2dt: frows: 1cols: 12dt: fCAD 模型网格/云通过 GICP 将模型数据与分割点云对齐,用于优化检测到的目标姿态。

2506_90492529的博客 2115

codeforces-165C-Another Problem on Strings

C. Another Problem on Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A string is binary, if it consists only of characte

看起来喜欢做枯燥事情的人或许只是更清楚自己想要什么罢了 585

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

165C.Another Problem on Strings

codeforces百题计划第一周(4) 观察 发现对于k!=0时,对于包含k个1的区间,第一个1左边的0和第k个1右边的0时可选可不选的,那么对于包含当前k个1的区间,找到包含最有0的所有不同情况就时对于包含当前k个1的所有区间 记录每个1的位置,cnt[i] 每个1左右0的个数,l[i],r[i] 对于每个包含k个1的区间就是 (r[i]+1)*(l[i-k+1]+1) ...

dieche3140的博客 208

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

CodeForces - 165C Another Problem on Strings (前缀)

题目链接:http://codeforces.com/problemset/problem/165/C点击打开链接 C. Another Problem on Strings time limit per test 2 seconds memory limit per test 256 megabytes input standar

xuejye的博客 664

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

CodeForces 165C Another Problem on Strings(组合)

A string isbinary, if it consists only of characters "0" and "1". Stringvis asubstringof stringwif it has a non-zero length and can be read starting from some position in stringw. Fo...

apk6909的博客 130

Another Problem on Strings-(CodeForces-165C)

*C. Another Problem on Strings* time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A string is binary, if it consists only of characters “...

lvy_yu_ET的博客 358

CodeForces 165C Another Problem on Strings(公式推导)

A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "

HJY 580

Another Problem on Strings CodeForces - 165C

Another Problem on Strings CodeForces - 165C A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can b

codeswarrior的博客 446

CodeForces 165C - Another Problem on Strings(思路)

题意:给定一个长度为 n (1 <= n <= 10^6 ) 的01串,求有多少连续子序列中包含 k 个 1。 从前往后记录到第 i 个元素存在了多少个 1,然后从前往后找个数 >= k 的元素,再找 - k 与 - k + 1 之间与多少个元素加入结果即可。 #include<cstdio> #include<cstring&gt...

dingyaosao9792的博客 156

CodeForces 165 C.Another Problem on Strings(尺取)

Description 给出一个010101串,问该串的恰有kkk个111的子串数量,两个子串只要在该串中位置不同则视为不同 Input 第一行一整数kkk,之后输入一个010101串s(1≤k,|s|≤106)s(1≤k,|s|≤106)s(1\le k,|s|\le 10^6) Output 输出满足条件的子串数 Sample Input 1 1010 Sample Outp...

ZSQ 386

codeforces 165-C. Another Problem on Strings(计数+尺取)

A string is binary, if it consists only of characters “0” and “1”.String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, s

HJ 800

codeForces 165C.Another Problem on Strings (二分+前缀和)

题意:给你一个整数K和一个只含01的字符串s,问你在这个字符串s中有多少个含有k个‘1’的子串。 思路:k和字符串长度都为10^6,所以暴力枚举所有子串不行,我们可先求一个前缀和统计到当前位置有多少个1,这样我们每次只需要用O(1)的负责度就可以知道任意两个位置之间(也就是某个子串中)有多少个‘1’了,然后我们枚举子串起点i,然后对于每一个起点寻找sum[i] + k - (s[i] - '0'

张松超的博客 682

CodeForces - 165C Another Problem on Strings —— 前缀和字串

C. Another Problem on Strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A string is binary, if it con

Lngxling的博客 434

CodeForces 165C Another Problem on Strings

Another Problem on Strings A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from so

galesaur_wcy 523
上一篇: Codeforces 467C George and Job DP
下一篇: Codeforces 415D Mashmokh and ACM DP
Masamiiiii
博客等级 码龄11年 32粉丝 625原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值