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 %I64dspecifier.

Sample Input

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

Hint

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

题意:在一个01字符川中找出 k 个1的子串的个数。

这题写麻烦了,对于 k 不为 0 的情况 设了 4个指针    (start, End) 表示 从start 到 End正好有 k 个 1,pre 是 start前面最近出现1的位置, last是End后面最近出现1的位置,所以 总个数就是 本身一个 + 前面start - pre - 1个 + 后面 last - End - 1个 + (前面) * (后面),然后 pre = start, start往后移动找下一个1,End = last,last往后移动找下一个1 ... 学的之前的尺规法,

对于k = 0的情况,就直接查 连续0的个数

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = + ;
char str[Max];
typedef long long LL;
void K()
{
int start = ;
LL cnt, sum;
cnt = sum = ;
int len = strlen(str);
while (start < len)
{
while (str[start++] == '')
cnt++;
if (cnt % ) // 为了防止爆精度,分情况
sum += (cnt + ) / * cnt;
else
sum += cnt / * (cnt + );
cnt = ;
}
printf("%I64d\n", sum);
}
int main()
{
int k;
scanf("%d", &k);
scanf("%s", str);
if (k == )
{
K();
}
else
{
int pre, start, End, last, cnt;
pre = start = End = last = cnt = ;
int len = strlen(str);
while (str[start] != '' && start < len)
start++;
if (start != len)
cnt = ;
pre = -;
End = start + ;
while (cnt < k && End < len)
{
if (str[End] == '')
cnt++;
End++;
}
last = End;
if (cnt == k)
End = End - ;
while (str[last] != '' && last < len)
last++;
LL sum = ;
while (End < len)
{
LL a = start - pre - ;
LL b = last - End - ;
sum += a + b + a * b + ;
pre = start;
while (str[++start] != '' && start < len); End = last;
while (str[++last] != '' && last < len);
}
printf("%I64d\n", sum);
}
return ;
}

CodeForces 165C Another Problem on Strings(组合)的更多相关文章

  1. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  2. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  3. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  4. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  5. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

  6. Codeforces 793C - Mice problem(几何)

    题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...

  7. CodeForces 687A NP-Hard Problem

    Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变 ...

  8. Codeforces Round #804 (Div. 2) C(组合 + mex)

    Codeforces Round #804 (Div. 2) C(组合 + mex) 本萌新的第一篇题解qwq 题目链接: 传送门QAQ 题意: 给定一个\(\left [0,n-1 \right ] ...

  9. Day8 - C - Another Problem on Strings CodeForces - 165C

    A string is binary, if it consists only of characters "0" and "1". String v is a ...

随机推荐

  1. Coding道场:第一次

    10/23日,我在部门内部进行了一次内部学习,使用目前流行的Coding Dojo(道场)方式,进行了TDD开发的演练.演练的题目如下:     有关Coding道场的介绍,请自行百度一下,我就不再多 ...

  2. WebForm(二)——控件和数据库连接方式

    一.简单控件 1.Label(作用:显示文字) Web中: <asp:Label ID="Label1" runat="server" Text=&quo ...

  3. SQL Server 2008 R2——使用FOR XML PATH实现多条信息按指定格式在一行显示

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  4. sql server: sql script

    select ProductGUID,ProductName,ProjectGUID from dbo.Product /* F637A079-E22B-4E50-87E9-000147B1B1F4 ...

  5. java实现串口通讯

    一. 准备工作 1. 点击此下载java串口通讯相关工具 2. 将RXTXcomm.jar放到  %JAVA_HOME%\jre\lib\ext\  目录下,工程中引入该jar包 3. 将rxtxSe ...

  6. markdown简要说明显示样式

    markdown 什么是markdown:     Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式.   Markdown具有一系列 ...

  7. sqlserver如何创建镜像图文教程(转)

    由于工作中需要做SQL的镜像异地备份,以前都没有研究过,百度了一个文章记录下,方便以后查询 转载地址:http://jingyan.baidu.com/article/d5c4b52b20843fda ...

  8. stm32定时器实现60秒定时秒表

    #include "led.h" #include "delay.h" #include "key.h" #include "sy ...

  9. Redis学习和环境搭建

    基本的redis教程,搭建,可以参照下面任一教程: 地址一:http://www.yiibai.com/redis/redis_quick_guide.html 地址二:http://www.runo ...

  10. [django]Django的css、image和js静态文件生产环境配置

    前言:在Django中HTML文件如果采用外联的方式引入css,js文件或者image图片,一般采用<link rel="stylesheet" href="../ ...