Operation Optimization
Time limit2sMemory limit256 MB
Find the shortest sequence of append-0, append-1, and self-doubling operations whose two-fold application to the empty string yields a given binary string S.
- Level
Hard8 of 10
- Topics
- String, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Hyunwook, who loves puzzle games, has found another interesting puzzle. The player starts with one empty string and can apply the following three operations to it.
- A: append 0 to the end of the current string.
- B: append 1 to the end of the current string.
- C: append a copy of the current string to the end of the current string.
The player can chain these operations in order to form a new operation . is written as the list of the operations used, in order. For example, is the operation that appends 10 to the end of the given string.
The puzzle gives a target string . Applying twice to the empty string must produce . That is, applying to the empty string gives some string, and applying once more to that string must give .
For example, if is 100100, then satisfies the condition. Applying to the empty string gives 100, and applying to 100 gives 100100.
The shorter is, that is, the fewer operations uses, the higher the score. Help Hyunwook find the length of the shortest that satisfies the condition.
Input
The first line contains the length of the target string (). The second line contains the target string . consists only of 0 and 1.
Output
Print the length of the shortest that satisfies the condition. If no such exists, print -1.