11127-Triple-Free Binary Strings (dfs + bit operation)

Source: Internet
Author: User

Problem J
Triple-Free Binary Strings
Input:
Standard Input

Output:Standard Output

A binary string consists of ones and zeros. Given a binary string T, if  there is no binary string S such that SSS (concatenate three copies of S together) is a substring of T, we say T is triple-free.
 
A pattern consists of ones, zeros and asterisks, where an asterisk(*) can be replaced by either one or zero. For example, the pattern 0**1 contains strings 0001, 0011, 0101, 0111, but not 1001 or 0000.
 
Given a pattern P, how many triple-free binary strings does it contain?
 
InputEach line of the input represents a test case, which contains the length of pattern, n (0
 
  
 
The input terminates when n=0.
 
OutputFor each test case, print the case number and the answer, shown below. 
Sample Input Output for Sample Input
4 0**1
5 *****
10 **01**01**
0
 
Case 1: 2
Case 2: 16
Case 3: 9
 

Question: Given a string, * represents the number of strings in 0, 1, and no three consecutive identical strings.

Train of Thought: Deep Search and bit operations, each time you determine that the current string has three duplicates

# Include
   
    
# Include
    
     
Const int N = 35; int n; char str [N]; bool judge (int state, int len) {int m = (1 <
     
      
> Len); int ss = (state & m); state = (state &(~ M)> len); if (s = ss & ss = state) return true; return false;} int dfs (int state, int len) {int ans = 0, s = state; for (int I = 0; I <= len-3; I ++) {if (len-I) % 3 = 0 & judge (s, (len-I)/3) return 0; s = (s &(~ 1)> 1);} if (len = n) return 1; if (str [len] = '0') ans + = dfs (state, len + 1); else if (str [len] = '1') ans + = dfs (state ^ (1 <
      
       
The string is returned, and the number can be expressed in binary.

Code:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.