You will get a binary string s (recall that if each character is 0 or 1, then a string is binary).
Let f (t) be the decimal representation of the integer t, written in binary form (may contain leading zeros). For example, f (011) = 3, f (00101) = 5, f (00001) = 1, (10) = 2, f (000) = 0 and f (000100) = 4.
If r-l + 1 = f (sl ... sr), the substrings sl, sl + 1, ..., sr are good.
For example, the string s = 1011 has 5 substrings: s1 ... s1 = 1, s3 ... s3 = 1, s4 ... s4 = 1, s1 ... s2 = 10, s2 ... s4 = 011.
Your task is to count the number of good substrings in the string s.
answer:
Find the number of substrings of equal value and length.
The method is to count the number of leading 0s in real time, and then use each number as a starting point to generate a binary string in real time. If you encounter a small length ratio and the number of leading 0s is greater than the difference between the two, it is legal.
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+100; string s; int main () { int T; scanf("%d",&T); while (T--) { cin>>s; int ans=0; int pre=0; for (int i=0;i<s.length();i++) { if (s[i]=='0') pre++; else { int r=i; int cnt=1; for (int j=0;j<18;j++) { if (cnt<=pre+(r-i+1)) ans++; if (r==s.length()-1) break; cnt = cnt * 2 + (s [++ r]- ' 0 ' ); } // Forward extension, if the length is not enough, use the leading 0 pre = 0 ; } } printf ( " % d \ n " , ans) ; } }