CF1217C

CF1217C

题意:

给定一个01串,一个good01串的定义是这个01串所代表的二进制数字刚好等于它的长度,允许前导零,问这个01串当中有几个good子串

解法:

枚举每一段连续的 $ 0 $ ,$ num_0 $ 为 $ 0 $ 的个数,后面的数值为 $ res $ ,只要 $ res geq len$ , $ len $ 是二进制下区间长度,并且 $ res leq len+num_0 $ ,则会产生答案,因为前导 $ 0 $ 可以匹配,但如果这样不行,直接break。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath> 

using namespace std; 

#define LL long long
#define N 200010

char ch[N]; 
int T,len;

int main() {
    scanf("%d",&T); 
    while(T--) {
        scanf("%s",ch + 1); 
        len = strlen(ch + 1); 
        int num_0 = 0; 
        LL ans = 0LL; 
        for(int i = 1 ; i <= len ; i++) {
	  	    if(ch[i] == '0') num_0++; 
		    else {
			    int res = 0; 
			    for(int j = i ; j <= len && res <= num_0 + (j - i + 1) ; j++) {
				    res = 2 * res + ch[j] - '0'; 
				    if (res >= (j - i + 1) && res <= (num_0 + j - i + 1)) ans++; 
				    else break; 
			    }
			    num_0 = 0; 
		    }
	    }
	    printf("%lld 
",ans);
    }
    //system("pause");
    return 0; 
}
原文地址:https://www.cnblogs.com/Repulser/p/11478392.html