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. SqlServer--delete、truncate 、Drop删除表的区别

    --删除表中的全部数据,自动编号不清零. --1.delete from Biao --删除表中的全部数据,自动编号清零. --2.truncate table Biao --truncate特点: ...

  2. params可变参数

    class Program { // params可变参数 //将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理. //params可变参数必须是形参列表中的最后一个元素. static ...

  3. W3School-CSS 表格实例

    CSS 表格实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CSS 内边距 (paddi ...

  4. PostgreSQL-function、trigger

    增加一个自动记录更新时间的触发器, 第一步,先写一个函数,返回触发器类型的 create function spam_keyword_update_trigger() returns trigger ...

  5. Linux下查找文件:which、whereis、locate、find 命令的区别

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.which       查看可执行文件的位置,通过环境变量查whereis    查看文件的位置,通过数据库查,每 ...

  6. Visual Studio SetSite failed for package [JavaScriptWebExtensionsPackage] 错误解决方案一则

    安装 AspNet5.ENU.RC1.exe Microsoft ASP.NET and Web Tools 2015 (RC) – Visual Studio 2015 打开VS后发生了错误 < ...

  7. 读 [The Root of Lisp]

    首先,在对 Lisp 有一丢丢的了解下读这篇文章会大大激发你学下去的欲望.之后可以看作者的著作<ANSI Common Lisp>. 想要体会一下 Lisp 的强大,本文是不二之选. Co ...

  8. AC日记——寻找道路 洛谷 P2296

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  9. 脑成像数据分析:Python工具包

    来源:SealHuang 脑成像技术已经成为认知科学和心理学研究领域中一种重要的研究手段,帮助研究者不断深入发掘我们脑中的秘密.伴随着研究的不断深入,各式各样的指标参数和分析方法也不断推陈出新,以迅雷 ...

  10. 代码覆盖工具(gcov、lcov)的使用

    一.安装 gcov:是随gcc一起发布的,并不需要独立安装:lcov:其他博客说是随ltp发布的,结果下载下ltp之后编译了10多分钟,最后也没见lcov,最后到sourceforge下载了lcov单 ...