Another Problem on Strings CodeForces - 165C

Android开发必备:TBS腾讯浏览服务静态集成完整指南(附Demo下载) 本文详细解析了Android开发中TBS腾讯浏览服务的静态集成方案,通过对比动态集成,深入剖析了其核心原理与适用场景。文章提供了从环境准备、资源获取、项目配置到代码初始化的完整指南,并重点介绍了WebView与文件预览等高级特性的实战应用。针对集成过程中的常见问题,提供了详尽的排查清单与性能调优建议,旨在帮助开发者构建极致稳定、不依赖网络的WebView与文件预览功能。文末附有经过验证的Demo项目可供下载参考。 阅读详情

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 be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.

You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1".

Input

The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.

Output

Print the single number — the number of substrings of the given string, containing exactly k characters "1".

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Example
Input
1
1010
Output
6
Input
2
01010
Output
4
Input
100
01010
Output
0
Note

In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".

In the second sample the sought substrings are: "101", "0101", "1010", "01010".

这道题一开始不会做,在网上看到一种很巧妙的方法。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[10000000];//num[i]实际上代表的是第i个1后面跟了几个0的个数+1(加1就是加上1字符本身也算一个),例如num[1] = 2            就代表第一个1后面有一个0加上1本身,所以num[1]为2,之后是第二个1
char s[10000000];//存字符串
int main(){
    int k;
    cin >> k >> s;
    long long ans = 0;
    int cnt = 0;//cnt代表第cnt个1,每次遇到新的1,cnt++
    int len = strlen(s);
    int i;
    memset(num,0,sizeof(num));
    num[0] = 1;
    for(i = 0; i < len; i++){//一位一位进行遍历
        if(s[i] == '1')cnt++;
        if(cnt >= k)ans += num[cnt-k];
        
        //当出现的1的个数大于等于要求的k了,就开始计算了
        //num[cnt-k]就是当前已经出现的1的总个数减去要求的个数,也就是固定有k个1的字符串长度后,往前在到达第k+1个1之  间0的个数就是包含这k个1的字符串所能构成的子串。
       //举个例子。k = 2时若串是00100101,假设到了最后一个1也就是第三个1,cnt=3>k=2,所以此时得到num[cnt-k] = num[1],num[1]是记录1后面的0个数+1,这样也就是我固定了后两个1即固定了串101,往前找,在第一个1之前有几个零是不是就可以有几种子串,当然还要加上不加0的它本身,这也是num[i]数组加1,num[0]初始化1的用意。      
       
        num[cnt]++;//储存第cnt个1后面0的个数加上1这个本身
    }
    cout << ans << endl;
    return 0;
}


MATLAB新手必看:5分钟搞定直流无刷电机Simulink仿真(附完整模型下载) 本文为MATLAB新手提供直流无刷电机Simulink仿真的快速入门指南,涵盖从环境配置到速度控制闭环实现的完整流程。通过详细的模块选择和参数设置说明,帮助读者5分钟内搭建基础仿真模型,并附赠完整模型下载,助力快速掌握电机控制仿真技术。 阅读详情

相关推荐

DeepSeek绝了!3分钟生成PPT,专治“五彩斑斓的黑“!!(保姆教程)

DeepSeek绝了!3分钟生成PPT,专治"五彩斑斓的黑"!!(保姆教程)

yizhen91y的博客 4万+

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

FreeRTOS-Plus-CLI在STM32F429上的高效移植与调试技巧

本文详细介绍了如何将FreeRTOS-Plus-CLI高效移植到STM32F429平台,并分享关键的调试技巧。内容涵盖使用HAL库进行底层驱动适配(特别是DMA+IDLE中断方案)、工程集成、自定义命令开发以及常见问题解决方案,旨在帮助开发者快速为嵌入式系统添加强大的命令行调试接口,提升开发与维护效率。

weixin_29224309的博客 338

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

Vivado IP核调参大师:高效配置RS编码器的避坑指南

本文深入解析Vivado中RS编码器IP核的高效配置方法,涵盖符号宽度选择、码长优化和AXI4-Stream接口控制等关键参数。重点指导如何避开常见陷阱,实现FPGA设计中的资源优化与性能平衡,提升RS编码在通信系统中的可靠性。

metaverse5vr的博客 225

TC3XX - MCAL知识点(三十三):基于EB Tresos配置Tc3xx的MCAL代码如何产生中心对称的三相六路发波波形

本文用于记录如何在英飞凌 tc3xx 芯片的应用开发中,借助 EB Tresos 配置工具生成的配置代码与 MCAL 源码相结合,进而实现三相六路中心对称互补的 PWM 输出效果。外部资源需求:EB Tresos23.0以上、Tasking\Hightec编译器、示波器\逻辑分析仪。

梦想技术家 1241

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

成对使用离轴抛物面反射镜的设计

成对使用离轴抛物面反射镜的设计

weixin_72050316的博客 642

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

基于STM32设计的太阳能智慧路灯_444

本文设计并实现了一种基于 STM32 的太阳能智慧路灯系统。系统以 STM32F103C8T6 单片机为核心,集成温湿度检测、空气质量检测、环境光照检测、行人检测和路灯故障检测等功能,实现对路灯运行环境和工作状态的实时感知。

478

交换机初始化配置详解

MGMT或称MGT(Management Port,管理端口)是一个可以连接计算机的三层主机端口,如下图所示。只需使用一条双绞线跳线,就可以将计算机的以太网端口与交换机的MGMT端口连接在一起,实现对交换机的配置与管理。也就是说,可以使用MGMT端口代替Console端口实现对交换机或交换机堆叠的网络管理。MGMT端口既可用于交换机的初始化,也可以在交换机初始化完成后,以Telnet、Web浏览器或Cisco CNA方式进行管理。当将计算机连接到独立的交换机时,连接方式如下图所示。

悦分享 2014

Another Problem on Strings

本题中‘0’不起计数作用,但对排列有影响 当k==1时 对数组进行for循环 从第一位开始遍历 到满足k个1为止,统计整体前后0的个数 k==1 例如:1010 第一个1产生的影响(1 10) 第二个1产生的影响(01 010 1 10) 1前面的0(包括1)记为x,后面的(1和0的个数)记为y //1*2 //2*2 再...

C_Dreamy的博客 763

实战指南:理解 ThreadLocal 原理并用于Java 多线程上下文管理

探讨如何基于实现一个高效的上下文管理组件,以解决多线程环境下的数据共享和上下文管理这些问题。通过具体的代码示例和实战展示如何为多线程编程提供一种简洁而高效的上下文管理方案。

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 11万+

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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

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

codeforces 165C Another Problem on Strings 双指针

题意:求对于给定01字符串,有多少个子串,其恰好包含了k个1 双指针,把每个1都找出来,统计这个1之后有多少个0 然后用乘法原理计算 #include #include #include #include #define rep(i, j, k) for(int i = j; i <= k; i++) using namespace std; typedef long

cadongllas的博客 404

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 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 examp

CS33sun的博客 361

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

点击打开链接 题意:给出串s,长度 尺取:以l为左端点成立的子串,若第一次在r成立 最后一次在y成立(r后的第一个1在y+1) 则个数为y-r+1 第一次在r成立,[l,r]成立 [l+1,r-1]肯定不成立  dp记录i后的第一个1 复杂都为O(2*n) #include using namespace std; typedef long long ll; const int

Masamiiiiii 430

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

CodeForces 165C Another Problem on Strings(递推)

题意:给出一个二进制串,统计有多少中含有k个一的子串。 思路:令答案为ans,首先用数组a记录1出现的位置,用cnt代表a的个数,当1的个数大于等于k时,ans += a[cnt - k - 1] - a[cnt - k] ;  注意:当k = 0时,特判。 #include #include #include #include #include #include #include #

fly_tzf的博客 639
上一篇: Burning Midnight Oil CodeForces - 165B
下一篇: Garden CodeForces - 915A
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值