首页 > 其他 > 详细

Educational Codeforces Round 72 (Rated for Div. 2) C题

时间:2019-09-06 17:36:44      阅读:215      评论:0      收藏:0      [点我收藏+]

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

4
0110
0101
00001000
0001000

Output

4
3
4
3

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

思路:先求每个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=0;
        int zero=0;
        int len=s.size();
        int sum=0;// 计算f函数 
        for(int i=0;i<len;i++){
            if(s[i]==0){// 1的前面0 的个数 
                zero++;
            }else{
                sum=0;
                int cnt=0;
                for(int j=i;j<len;j++){
                    sum=sum*2+s[j]-0;// f()函数值 
                    cnt++;// 长度 
                    if(sum>=len+2){// f()>=字符串长度 
                        break;
                    }
                    if(cnt+zero>=sum){ // 满足条件 
                        ans++;
                    }
                }
                zero=0;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

Educational Codeforces Round 72 (Rated for Div. 2) C题

原文:https://www.cnblogs.com/pengge666/p/11476939.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!