codeforces 之 Another Problem on Strings
来源:互联网 发布:淘宝上信用卡套现吗 编辑:程序博客网 时间:2024/05/21 11:10
赶脚自己逗比了,从昨天晚上开始刷这道题,前前后后交了12遍,最后实在是找不到错哪了,就是抱着试试看的态度改了一下数据类型,发现竟然是跪在了LONG LONG 这个整数类型上,这以后没法说自己是搞ACM的了。。。。。
解题思路:
给你一个只包含0和1的字符串,在这个字符串中你找一下包含k个1的子字符串的个数,所以当k=0时,或者在开头或者在任意两个1之间或者在结尾0的个数为co,经分析可知,0可以单个出现,可以最少两个的出现,可以找到规律:(co+1)*co/2; 当k > 0时,就要统计下在这k个1前面有a个0,后面有b个0,经分析可知:这k个1可以跟前面a个0一个或者多个0结合,同理可以跟后面的,也可以前后结合,可以找到规律:a+b+a*b。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long a[1000010]={0};
char s[1000010];
int main()
{
long long i,k,len,co=0;
cin>>k;
long long sum=0;
cin>>s;len = strlen(s);
for(i=0; s[i]; i++) {
if(s[i] == '1') a[co++] = i;
}
if(k>co) cout<<0<<endl;
else if(k==0) {
if(co==0) {
sum = (len+1)*len/2;
cout<<sum<<endl;
return 0;
}
sum=(a[0]*(a[0]+1))/2;
for(i=1; i<co; i++) sum+=((a[i]-a[i-1]-1)*(a[i]-a[i-1]))/2;
sum+=((len-a[co-1])*(len-a[co-1]-1))/2;
cout<<sum<<endl;
}
else {
sum=co-k+1;
for(i=k-1; i<co; i++) {
long long b,c;
if(i==k-1) {
b = a[0];
}
else {
b = a[i-k+1] - a[i-k]-1;
}
if(i==co-1) {
c = len-a[co-1]-1;
}
else {
c = a[i+1] - a[i]-1;
}
sum+=(b+c+b*c);
}
cout<<sum<<endl;
}
return 0;
}
- codeforces 之 Another Problem on Strings
- Another Problem on Strings CodeForces
- CodeForces 165C - Another Problem on Strings
- CodeForces 165C - Another Problem on Strings
- codeforces-165C-Another Problem on Strings
- Another Problem on Strings
- 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
- Codeforces 165C Another Problem on Strings 【二分】
- codeforces 165C Another Problem on Strings 双指针
- CodeForces 165C Another Problem on Strings(公式推导)
- cf165c Another Problem on Strings
- codeforce 165C Another Problem on Strings
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- codeforces 165-C. Another Problem on Strings(计数+尺取)
- 【codechef】Yet Another Problem On Strings (找最优解)
- android基础笔记:notification通知(android3.0以上版本适用)
- JVM简介
- nginx
- 系统开发中寻找免费且好用的甘特图控件
- IP-route管理路由
- codeforces 之 Another Problem on Strings
- 容斥原理模板(续)
- Android UI之消息队列
- Redis在windows下的安装使用
- Java中的动态代理
- STM32---按键学习
- Struts2过程图
- C语言关键字”volatile”的意义(或重要性)
- 《赋得古原草送别》 白居易