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. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    从供应商手中接手一个云平台(Windwos Azure)上的MySQL数据库,登录数据库时遇到错误: $mysql -uroot -p Enter password: ERROR 2002 (HY00 ...

  2. SQL SERVER 中如何用脚本管理作业

    在SQL SERVER中用脚本管理作业,在绝大部分场景下,脚本都比UI界面管理作业要高效.简洁.打个简单的比方,如果你要查看作业的运行时长,如果用UI界面查看,100个作业,你就得在历史记录里面至少查 ...

  3. 使用eclipse开发Morphline的Java代码段

    背景:morphline是一个轻量级的etl工具.除了提供标准化的方法之外,还可以定制化的开发java片段.定制化的java片段会在加载时被作为一个独立的类编译,对源数据作处理. morphline关 ...

  4. 大数据系列(1)——Hadoop集群坏境搭建配置

    前言 关于时下最热的技术潮流,无疑大数据是首当其中最热的一个技术点,关于大数据的概念和方法论铺天盖地的到处宣扬,但其实很多公司或者技术人员也不能详细的讲解其真正的含义或者就没找到能被落地实施的可行性方 ...

  5. CentOS安装Oracle数据库详细介绍及常见问题汇总

    一.安装前准备 1.软件硬件要求 操作系统:CentOS 6.4(32bit)Oracle数据库版本:Oracle 10g(10201_database_linux32.zip)最小内存:1G(检查命 ...

  6. css 使容器宽度适应内容宽

    p{ width:intrinsic; width: -moz-max-content; width: -webkit-max-content; float:right; width:auto; /* ...

  7. ik扩展支持Solr配置

    扩展ik原生代码:public class IKAnalyzerTokenizerFactory extends TokenizerFactory{ private boolean useSmart; ...

  8. 【2016-10-17】【坚持学习】【Day9】【反射】

    3个主要命名空间 System.Type System.Reflection System.Reflection.Assembly 2个主要类 System.Type System.Reflectio ...

  9. 转: 深入理解Linux修改hostname

    from: http://www.cnblogs.com/kerrycode/p/3595724.html 写的相当详细!!! 深入理解Linux修改hostname 2014-03-12 10:17 ...

  10. python高级之装饰器

    python高级之装饰器 本节内容 高阶函数 嵌套函数及闭包 装饰器 装饰器带参数 装饰器的嵌套 functools.wraps模块 递归函数被装饰 1.高阶函数 高阶函数的定义: 满足下面两个条件之 ...