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", "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的个数

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 const int Max = 1000000 + 10;
 7 char str[Max];
 8 typedef long long LL;
 9 void K()
10 {
11     int start = 0;
12     LL cnt, sum;
13     cnt = sum = 0;
14     int len = strlen(str);
15     while (start < len)
16     {
17         while (str[start++] == '0')
18             cnt++;
19         if (cnt % 2)  // 为了防止爆精度,分情况
20             sum += (cnt + 1) / 2 * cnt;
21         else
22             sum += cnt / 2 * (cnt + 1);
23         cnt = 0;
24     }
25     printf("%I64d\n", sum);
26 }
27 int main()
28 {
29     int k;
30     scanf("%d", &k);
31     scanf("%s", str);
32     if (k == 0)
33     {
34         K();
35     }
36     else
37     {
38     int pre, start, End, last, cnt;
39     pre = start = End = last = cnt = 0;
40     int len = strlen(str);
41     while (str[start] != '1' && start < len)
42         start++;
43     if (start != len)
44         cnt = 1;
45     pre = -1;
46     End = start + 1;
47     while (cnt < k && End < len)
48     {
49         if (str[End] == '1')
50             cnt++;
51         End++;
52     }
53     last = End;
54     if (cnt == k)
55         End = End - 1;
56     while (str[last] != '1' && last < len)
57         last++;
58     LL sum = 0;
59     while (End < len)
60     {
61         LL a = start - pre - 1;
62         LL b = last - End - 1;
63         sum += a + b + a * b + 1;
64         pre = start;
65         while (str[++start] != '1' && start < len);
66 
67         End = last;
68         while (str[++last] != '1' && last < len);
69     }
70     printf("%I64d\n", sum);
71     }
72     return 0;
73 }
View Code

 

posted @ 2016-05-10 19:06  zhaop  阅读(488)  评论(0编辑  收藏  举报