C. The Number Of Good Substrings

Problem Description:

You are given a binary string s (recall that a string is binary if each character is either 0 or 1).
Let f(t) be the decimal representation of integer t written in binary form (possibly with leading zeroes). For example f(011)=3,f(00101)=5,f(00001)=1,f(10)=2,f(000)=0 and f(000100)=4.
The substring sl,sl+1,…,sr is good if r−l+1=f(sl…sr).
For example string s=1011 has 5 good substrings: s1…s1=1, s3…s3=1, s4…s4=1, s1…s2=10 and s2…s4=011.
Your task is to calculate the number of good substrings of string s.
You have to answer t independent queries.
Input
The first line contains one integer t (1≤t≤1000) — the number of queries.
The only line of each query contains string s (1≤|s|≤2⋅105), consisting of only digits 0 and 1.
It is guaranteed that ∑i=1t|si|≤2⋅105.
Output
For each query print one integer — the number of good substrings of string s.

Input


Output


题意:求子串的个数。子串需要满足:长度与二进制数相同。

思路:先求每个1前面的0的个数 ,分别从1当前位置开始遍历字符串.计算f()函数值是否满足条件(长度==f()函数值).

AC代码:

#include<bits/stdc++.h>

using namespace std;
#define int long long
signed main(){
int _;
cin>>_;
while(_--){
string s;
cin>>s;
int ans=;
int zero=;
int len=s.size();
int sum=;// 计算f函数
for(int i=;i<len;i++){
if(s[i]==''){// 1的前面0 的个数
zero++;
}else{
sum=;
int cnt=;
for(int j=i;j<len;j++){
sum=sum*+s[j]-'';// f()函数值
cnt++;// 长度
if(sum>=len+){// f()>=字符串长度
break;
}
if(cnt+zero>=sum){ // 满足条件
ans++;
}
}
zero=;
}
}
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 归一化变换 Normalizing transformations
  2. C语言中的参数传递
  3. Linux之绝处逢生------SysRq
  4. bat批量删.svn
  5. 2013年7月份第4周51Aspx源码发布详情
  6. 在Visual Studio 2010 中创建类库(dll)
  7. ASP.NET MVC轻教程 Step By Step 8——路由
  8. mysql 循环插入100w
  9. Ubuntu下安装vmware 9.0 + 注册码
  10. jQuery的$.ajax方法响应数据类型有哪几种?本质上原生ajax响应数据格式有哪几种,分别对应哪个属性?
  11. mac 下 clang++ 找不到头文件 stdlib.h
  12. 如何kill掉tomcat服务
  13. MT【223】二次函数最大最小
  14. AFP溢出攻击模块afp/loginext
  15. 微软Power BI报表服务器学习总览
  16. H3C交换机配置的备份与恢复(TFTP方法)
  17. java synchronized 关键字原理
  18. 安卓Camera APP
  19. Liunx常用的100条命令汇存
  20. Java 加密Excel文件(打开时需输入密码)

热门文章

  1. Docker从国内代理下载镜像
  2. NeurIPS 2018 中的贝叶斯研究
  3. 怎样理解构造函数的原型对象prototype
  4. [Vue]vue-router嵌套路由(子路由)
  5. Linux 数据库MySql 安装配置教程!
  6. MiniUI学习笔记一【转】
  7. FastDFS高可用集群架构配置搭建及使用
  8. 在Linux下执行Jmeter脚本
  9. undefined reference to `udev_device_get_action'
  10. crunch离线密码生成