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 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
1
1010
6
2
01010
4
100
01010
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=0的情况,左指针可以小优化一下,将连续的0对右指针只计算一次
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e6+; int k;
char buf[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
cin >> k >> buf;
LL ans = ;
int n = strlen(buf);
LL num = ;
int l = , r = , t = , r1 = ;
if(k == ) {
for(int i = ; i < n; ++i) {
if(buf[i] == '')
num++;
else {
ans += (LL)(num*(num+) / );
num = ;
}
}
ans += (LL)(num*(num+) / );
} else {
if(buf[r] == '') t++;
while(l < n) {
while(r < n) {
if(t == k) break;
r++;
if(buf[r] == '') t++;
}
if(r >= n) break;
if(r1 <= r) {
r1 = r+;
while(r1 < n && buf[r1] == '') r1++;
}
LL L = ;
while(buf[l++] == '') L++;
ans += (LL)(r1 - r) * L;
t--;
}
} cout << ans << "\n";
return ;
}
Day8 - C - Another Problem on Strings CodeForces - 165C的更多相关文章
- CodeForces 165C Another Problem on Strings(组合)
A string is binary, if it consists only of characters "0" and "1". String v is a ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
http://codeforces.com/contest/868/problem/D 优化:两个串合并 原有状态+ 第一个串的尾部&第二个串的头部的状态 串变为第一个串的头部&第二个 ...
- A. Yet Another Problem with Strings 分块 + hash
http://codeforces.com/gym/101138/problem/A 感觉有一种套路就是总长度 <= 某一个数的这类题,大多可以分块 首先把集合串按长度分块,对于每一个询问串, ...
- Problem for Nazar CodeForces - 1151C (前缀和)
Problem for Nazar Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for ...
- Mike and strings CodeForces - 798B (简洁写法)
题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...
- Mike and strings CodeForces - 798B (又水又坑)
题目链接 题意:英语很简单,自己取读吧. 思路: 既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞. 本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行) ...
- codeforces 578a//A Problem about Polyline// Codeforces Round #320 (Div. 1)
题意:一个等腰直角三角形一样的周期函数(只有x+轴),经过给定的点(a,b),并且半周期为X,使X尽量大,问X最大为多少? 如果a=b,结果就为b 如果a<b无解. 否则,b/(2*k*x-a) ...
- codeforces 559b//Equivalent Strings// Codeforces Round #313(Div. 1)
题意:定义了字符串的相等,问两串是否相等. 卡了时间,空间,不能新建字符串,否则会卡. #pragma comment(linker,"/STACK:1024000000,102400000 ...
随机推荐
- 如何安装部署和优化Tomcat?(Tomcat部署和优化与压测,虚拟主机配置,Tomcat处理请求的过程)
文章目录 前言 一:Tomcat安装部署 1.1:Tomcat简介 1.2:Tomcat核心组件 1.3:Tomcat处理请求的过程 1.3.1:请求过程基本解释 1.3.2:请求过程详细解释 1.4 ...
- iOS之Xcode提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package
https://www.jianshu.com/p/6d465a0ea58e 这句英文翻译过来就是: 无法继续交付:现有的传输程序实例目前正在上载此包 原因:上传的动作被记录在UploadToken中 ...
- 电影推荐算法---HHR计划
1,先看FM部分. 2,看看冷启动. 0,热门召回源. 1,男女召回源,年龄召回源,职业召回源,score最高. 2,男女年龄职业相互组合: 3,存入redis.天级别更新. 3,召回+排序先搞懂. ...
- vue配置、创建项目及运行
首先安装Node.js, npm i -g cnpm --registry=https://registry.npm.taobao.org 安装镜像 安装以后cnpm可以代替npm cnpm i -g ...
- redis 高级学习和应用场景
redis 高级学习 1.redis 复制 2.redis 集群 3.哨兵机制 4.spring 与哨兵结合 5.数据恢复与转移 6.redis 的阻塞分析 redis 实战 1. 数据缓存(热点数据 ...
- Codeforces1301C. Ayoub's function
本题的收获是,要学会反向思维,正向找包含1的太多,我们就反向找,全排列-只有0的不满足题意的就是答案,一共有n-m个0,m个1,插空法,一共有m+1个地方可以插入0序列,总排列数为(n+1)*n/2, ...
- 单表千亿电信大数据场景,使用Spark+CarbonData替换Impala案例
[背景介绍] 国内某移动局点使用Impala组件处理电信业务详单,每天处理约100TB左右详单,详单表记录每天大于百亿级别,在使用impala过程中存在以下问题: 详单采用Parquet格式存储,数据 ...
- JSON传输图片帮助类
JSON传输图片帮助类 2014-05-27 16:11:22| 分类: Java | 标签:解决方案 java json |举报|字号 订阅 原理:将图片转换为字节流,再将字节流用 ...
- [Linux] day04——Linux 入门
Linux 入门 =================Gnome 基于GTK图形库 速度快 稳定性好 专注与桌面本身 偏商务 RedHat企业版默认桌面KDE 基于QT 速度慢 软件多 习惯接近win ...
- 「Luogu P3820 小D的地下温泉」
这道题的考点比较多. 前置芝士 BFS(DFS),这两种算法在这道题中并没有什么特别突出的地方,基本就是自己看心情写(本文以DFS为准,所以我心情是好是坏呢?) 连通块,可以将每一个温泉看作一个连通块 ...