Codeforces 165C Another Problem on Strings 【二分】
2016-05-07 14:51
399 查看
题目链接:Codeforces 165C Another Problem on Strings
C. Another Problem on Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 %I64d specifier.
Examples
input
1
1010
output
6
input
2
01010
output
4
input
100
01010
output
0
Note
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”.
题意:问你严格有k个1的子串数目。
思路:预处理前缀和,然后二分。
AC代码:
C. Another Problem on Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 %I64d specifier.
Examples
input
1
1010
output
6
input
2
01010
output
4
input
100
01010
output
0
Note
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”.
题意:问你严格有k个1的子串数目。
思路:预处理前缀和,然后二分。
AC代码:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> #define CLR(a, b) memset(a, (b), sizeof(a)) #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 1e6 +10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void add(LL &x, LL y) { x += y; x %= MOD; } char str[MAXN]; int sum[MAXN]; int Find(int l, int r, int p, int v) { int ans = r + 1; while(r >= l) { int mid = (l + r) >> 1; if(sum[mid] - sum[p-1] >= v) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; } int main() { int k; while(scanf("%d", &k) != EOF) { scanf("%s", str+1); int len = strlen(str+1); sum[0] = 0; for(int i = 1; i <= len; i++) { sum[i] = sum[i-1] + (str[i] == '1'); } LL ans = 0; for(int i = 1; i <= len; i++) { int s = Find(i, len, i, k); if(s == len + 1) continue; int t = Find(i, len, i, k+1); ans += t - s; } printf("%lld\n", ans); } return 0; }
相关文章推荐
- Ubuntu网络配置
- Ubuntu配置ssh server
- 获取root登录ubuntu桌面的方法
- NetBSD nfs 简单配置
- NetBSD多文件结构Makefile编写
- vs2008文件类型介绍
- Visual Studio 里 Solution 和 Project 的区别[转]
- Codeforces 165B Burning Midnight Oil 【二分】
- visual studio 2008 自定义头文件设置
- 第十周第十一周上机实践项目-项目1-(1)-点-圆-圆柱类族的设计
- XMLHttpRequest的属性
- android裁剪图片
- 预定义字符
- NetBSD时间设置
- ssh出现的问题
- 10G以太网核心技术【转】
- 以太网技术基础
- MII接口【转】
- vi命令大全【转】
- NetBSD3.0 samba 配置