[Problem description] the input is a string consisting of only 0/1 characters and cannot contain three consecutive identical substrings.
Input: the length of the string is n (n <= 40 ).
Output: the number of all strings that meet the condition.
[Sample input]
2
[Sample output]
4
[Problem Analysis]
Set the length of the 0/1 sequence to L, X, Y, and Z as the first pointer of the Third, Second, and first substring respectively;
Initial: x = L; y = L-1; Z = L-2;
If three numbers are different, Dec (x, 1), Dec (Y, 2), Dec (z, 3 );
Until (A [Z]... A [Y-1] = A [y]... A [x-1] = A [x]... A [l]) or (z <= 0)
Open three pointers to determine if the substrings are equal;
============================================ ==================================
Optimization: Use symmetry. For example, if n = 6, 011001 does not contain three consecutive identical substrings. Therefore, 100110 does not include three consecutive identical substrings;
Therefore, you only need to search for a sequence whose 1st bits are 0, and multiply the number of obtained solutions by 2 to get the result.
String generation method: backtracking.
At the beginning, a [1] = 0, and then try (A [2]). The order is set to 0 and then 1. If the condition is met after 0, enter the next digit. Otherwise, change it to 1, if the condition is still not met, go back to the previous one. If the condition is met, enter the next one.
Number of solutions + 2 when generating a string;
VaR A: array [1 .. 40] of byte; // 0/1 sequence tot: longint; L: integer; // bit pointer N: integer; function judge (L: integer): Boolean; var x, y, z: integer; R, Q, P: integer; begin X: = L; Y: = L-1; Z: = L-2; // the pointer initializes while z> 0 do begin P: = x; Q: = y; R: = z; while (r <Y) and (A [p] = A [Q]) and (A [Q] = A [R]) Do begin Inc (p); Inc (Q); Inc (r); end; // traverse the sub-string if r = y then exit (false); // if the three sub-strings are identical, false Dec (x, 1) is returned; Dec (Y, 2 ); dec (z, 3); end; exit (true); end; procedure search (L: integer); begin if l> N then begin Inc (TOT, 2); exit; end; A [l]: = 0; if Judge (l) then search (L + 1); A [l]: = 1; if Judge (l) then search (L + 1); end; begin readln (n); A [1]: = 0; L: = 1; Tot: = 0; search (2 ); writeln (TOT); end.