C. The Number Of Good Substrings 【 Educational Codeforces Round 72 (Rated for Div. 2) C】【思维】

题目链接

题目大意

给你一个二进制01串,问你这个串有多少个好子串,设子串的起始和终止下标为 l 和 r ,那么好子串的定义为二进制子串换为十进制的数是等于(r-l+1)的

解题思路

因为这个串最长2e5,所以枚举的长度不超过18,但是因为有前导零的存在,我们不能只枚举长度为18的区间
我们用一个res来记录一下每个1前面有多少个前导零,如果当前的二进制数是小于等于res+r-l+1&&大于r-l+1的时候,就肯定存在一个好子串,也就是当前二进制数是大于不带前导零的长度的,但是长度可以用res个前导零补,所以最大的长度小于res+r-l+1的

比赛的时候想到了枚举长度肯定是很小的,但是不知道怎么处理前导零,然后就… … …

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int N=2e5+5;
char s[N];
int e[N];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        for(int i=1;i<=len;i++)
            e[i]=s[i]-'0';
        int ans=0,res=0;
        for(int i=1;i<=len;i++)
        {
            if(e[i]==0)
                res++;
            else
            {
                int xx=0;
                for(int j=i,k=1;j<=len&&k<=19;j++,k++)
                {
                    xx=xx*2+e[j];
                    if(xx<=res+(j-i+1)&&xx>=(j-i+1))
                        ans++;
                }
                res=0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The `implode()` function in PHP is used to join elements of an array into a string with a specified separator. It takes two parameters: the first parameter is the separator that will be used to join the array elements, and the second parameter is the array of elements to be joined. The function returns a string that is formed by joining the elements of the array with the specified separator. For example, if we have an array of strings like this: ``` $arr = array('apple', 'banana', 'pear'); ``` We can join the elements of the array into a string using the `implode()` function like this: ``` $str = implode(',', $arr); ``` This will create a string like this: ``` "apple,banana,pear" ``` The `explode()` function, on the other hand, is used to split a string into an array of substrings using a specified separator. It takes two parameters: the first parameter is the separator that will be used to split the string, and the second parameter is the string to be split. The function returns an array of substrings. For example, if we have a string like this: ``` $str = "apple,banana,pear"; ``` We can split the string into an array of substrings using the `explode()` function like this: ``` $arr = explode(',', $str); ``` This will create an array like this: ``` array('apple', 'banana', 'pear') ``` So, basically, the `implode()` function joins an array of elements into a string using a specified separator, while the `explode()` function splits a string into an array of substrings using a specified separator.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值