codeforce 165C Another Problem on Strings
来源:互联网 发布:淘宝达人申请大v认证 编辑:程序博客网 时间:2024/05/18 02:10
原题链接
题意
给定一个只含0或者1的字符串,给定整数K(0<=K<=1e6),求字符串中1的数目恰好为K的子串数量。
题解
形如100……001这样的子串符合题意,在两端加上0所得到的子串也符合题意,很明显是乘法原理
要求子串中1的个数恰好为K个,所以枚举子串起点,终点也就随之固定
预处理出1的位置数组id,用pre[i]记录第i个1和第i-1个1之间的0的个数,然后扫一遍OK。
有个坑,k==0需要额外处理
#include<bits/stdc++.h>using namespace std;typedef long long LL;const int maxn=1e6;char s[maxn+5];LL pre[maxn+5];int id[maxn+5];int main(void){int k;scanf("%d",&k);scanf("%s",s);int len=strlen(s);int cnt=0;for (int i=0;i<=len-1;++i){if (s[i]=='1') id[++cnt]=i;}id[0]=-1;id[cnt+1]=len;for (int i=1;i<=cnt+1;++i){pre[i]=id[i]-id[i-1]-1;}if (cnt<k){printf("0\n");return 0;}else if (k>=1){LL ans=0;for (int s=1;s<=cnt-k+1;++s){int t=s+k-1;ans+=(pre[s]+1)*(pre[t+1]+1);}printf("%I64d\n",ans);}else if (k==0){LL ans=0;for (int i=1;i<=cnt+1;++i){LL temp=(pre[i]*(pre[i]+1))/2;ans+=temp;}printf("%I64d\n",ans);}}
0 0
- codeforce 165C Another 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 【二分】
- codeforces 165C Another Problem on Strings 双指针
- CodeForces 165C Another Problem on Strings(公式推导)
- Another Problem on Strings
- cf165c Another Problem on Strings
- Another Problem on Strings CodeForces
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces Round #112 (Div. 2) C. Another Problem on Strings
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- codeforces 165-C. Another Problem on Strings(计数+尺取)
- codeforces 之 Another Problem on Strings
- C. Another Problem on Strin
- codeforces Another Problem on Strings 记录一个不慎
- oracle导入dmp文件命令、sql文件命令, Oracle用户的新增、修改、删除及授权
- Android——消息机制
- 微软的基于webgl的H5 3D游戏引擎
- Windows下CURL扩展无效之终极解决办法。
- Tomcat 内存溢出,堆栈配置各种调整
- codeforce 165C Another Problem on Strings
- 机器学习中怎样的特征才是好特征
- Hadoop之MapReduce改进的计数单词(八)
- LoadRunner入门
- 多线程 —— 多线程在WEB开发中的应用相关问题
- HTML5触摸事件(多点触控、单点触控)Demo
- HDU 2007 平方和与立方和(水题)
- 函数调用的本质与函数指针
- AndroidStudio 编译 XposedBridge 54版