#C765. Binary Strings without Consecutive Ones
Binary Strings without Consecutive Ones
Binary Strings without Consecutive Ones
You are given a positive integer n and you must generate all possible binary strings of length n such that the string does not contain two consecutive '1's. This problem is a classic exercise in backtracking. The order of output should follow the order of generation from the recursive process.
Constraints: The input is a single integer n (with n ≥ 1), and you need to consider all valid combinations.
Example:
- For n = 2, the valid binary strings are: "00", "01", "10". (Note: "11" is invalid because it contains consecutive 1's.)
- For n = 3, the valid strings are: "000", "001", "010", "100", "101".
Use an efficient backtracking method to generate the strings.
inputFormat
The input consists of a single line containing an integer n representing the length of the binary strings to be generated.
outputFormat
Output all valid binary strings that do not contain consecutive 1's. Each valid binary string should be printed on a separate line in the order they are generated.
## sample2
00
01
10
</p>