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 is read starting from some posit Ion in String W . For example, string "010" have six substrings: "0", " Span class= "Tex-font-style-tt" >1 "," 0 "," 01 "," Span class= "Tex-font-style-tt" >10 "," 010 ". Substrings is considered different if their positions of occurrence is 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 6 characters.
Output
Print the single number-the number of substrings of the given string, containing exactly K Characters " 1 ".
%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 is: "1", "1", "Ten", "", "Ten", "010".
In the second sample the sought substrings is: "101", "0101", "1010", "01010".
Test instructions: Find the number of K 1 substrings in a 01-character Fuchuan.
This inscribed trouble, for K is not 0 of the case set 4 pointers (start, end) indicates that from start to end exactly K 1,pre is the position of the most recent 1, last is the end of the last 1 position, so the total number is the Body One + front start-pre-1 + rear last-end-1 + (front) * (back), then pre = start, start moves backwards to find the next 1,end = last,last Move backward to find the next 1 ... Prior to the study of the ruler law,
For the case of k = 0, directly check the number of consecutive 0
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5 using namespacestd;6 Const intMax =1000000+Ten;7 CharStr[max];8typedefLong LongLL;9 voidK ()Ten { One intStart =0; A LL cnt, sum; -CNT = SUM =0; - intLen =strlen (str); the while(Start <len) - { - while(str[start++] = ='0') -cnt++; + if(CNT%2)//in order to prevent explosion accuracy, the sub-conditions -Sum + = (cnt +1) /2*CNT; + Else ASum + = CNT/2* (CNT +1); atCNT =0; - } -printf"%i64d\n", sum); - } - intMain () - { in intK; -scanf"%d", &k); toscanf"%s", str); + if(k = =0) - { the K (); * } $ ElsePanax Notoginseng { - intPre, start, End, last, CNT; thePre = start = End = last = CNT =0; + intLen =strlen (str); A while(Str[start]! ='1'&& Start <len) thestart++; + if(Start! =len) -CNT =1; $Pre =-1; $End = start +1; - while(CNT < K && End <len) - { the if(Str[end] = ='1') -cnt++;Wuyiend++; the } -Last =End; Wu if(CNT = =k) -End = End-1; About while(Str[last]! ='1'&& Last <len) $last++; -LL sum =0; - while(End <len) - { ALL A = Start-pre-1; +LL B = last-end-1; theSum + = a + B + A * b +1; -Pre =start; $ while(Str[++start]! ='1'&& Start <len); the theEnd =Last ; the while(Str[++last]! ='1'&& Last <len); the } -printf"%i64d\n", sum); in } the return 0; the}View Code
Codeforces 165C Another problem on Strings (combination)