CodeForces 165CAnother Problem on Strings(递推)
来源:互联网 发布:nginx反向代理参数说明 编辑:程序博客网 时间:2024/06/16 20:34
题意:给出一个二进制串,统计有多少中含有k个一的子串。
思路:令答案为ans,首先用数组a记录1出现的位置,用cnt代表a的个数,当1的个数大于等于k时,ans += a[cnt - k - 1] - a[cnt - k] ;
注意:当k = 0时,特判。
#include <cstdio>#include <algorithm>#include <iostream>#include<vector>#include<cmath>#include<set>#include<cstring>#include<map>using namespace std;const int maxn = 22222;const int maxt = 100200;const int INF = 0x3f3f3f3f;const int mod = 1e9 + 7;const double pi = acos(-1.0);typedef long long ll;char s[1000010];int a[1000010];int main(){ int k; scanf("%d%s", &k, s); int len = strlen(s); int cnt = 0; ll ans = 0; a[0] = -1; if(k == 0){ for(int i = 0; i < len; ++i){ if(s[i] == '0') cnt++; else{ ans += (ll)cnt * (cnt + 1) / 2; cnt = 0; } } if(cnt){ ans += (ll)cnt * (cnt + 1) / 2; } cout << ans <<endl; return 0; } for(int i = 0; i < len; ++i){ if(s[i] == '1') a[++cnt] = i; if(cnt >= k){ ans += (ll)(-a[cnt - k] + a[cnt - k + 1]); } } printf("%I64d\n", ans); return 0;}
1 0
- CodeForces 165CAnother Problem on Strings(递推)
- CodeForces 165C Another Problem on Strings(公式推导)
- CodeForces 165C - Another Problem on Strings
- CodeForces 165C - Another Problem on Strings
- codeforces-165C-Another Problem on Strings
- Another Problem on Strings CodeForces
- Codeforces 165C Another Problem on Strings 【二分】
- codeforces 165C Another Problem on Strings 双指针
- codeforces 165-C. Another Problem on Strings(计数+尺取)
- codeforces 之 Another Problem on Strings
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- codeforces Another Problem on Strings 记录一个不慎
- Codeforces Round #112 (Div. 2) C. Another Problem on Strings
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- codeforce 165C Another Problem on Strings
- Another Problem on Strings
- uva11081 - Strings(递推)
- 经常在类名那有错,修改后,经常加了private static final long serialVersionUID = 1L; 这个是什么意思,加了有什么用?
- Hadoop 2.7.2 linux分布式环境配置(centos 6.8 32bit)
- 题目1175:打牌
- 操作系统学习笔记(六)
- 分类算法之决策树(Decision tree)
- CodeForces 165CAnother Problem on Strings(递推)
- poj3020_最小覆盖路径(顶点个数-二分图最大匹配数)
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- 【精】给 Android 开发者的 RxJava 详解
- XML和Schema命名空间详解(转载)
- 德莱联盟[判断线段相交]
- jvm读书笔记之内存区域
- SQL四大语言:DDL DML DCL TCL
- 第一个Windows程序