After each move, the number of ones in the string increases by . So the game is finite.
To solve the problem, let’s make pairs of characters: . Each pair of characters should be equal to form a palindrome. Initially, there are possible types of pairs: , and (or ). If is odd, there is a single character at the center of the string which is either or . Let the count of each type of pairs be , and respectively and be the central character which is or . The string become a palindrome when is . There will be no valid move when or where and are number of zeros and ones in the string respectively.
when n is even.
when n is odd.
Now, the problem can be solved using dynamic programming where the states are , and (if is odd). which is extracted from the states. Let’s define each state as a winning state or a losing state. The state from which the current player (who will move from this state) always wins by playing optimally, is a winning state, otherwise, it’s a losing state.
The state where no valid move exists or the string is a palindrome, it’s a losing state. From any state, if we can reach at least one losing state after a single move, then the state is a winning state. Because the current player will choose the move such that the opponent gets a losing state. If all states we can reach from the current state are winning states, then the current state is a losing state. Because, no matter which move the current player chooses, the opponent will get a winning state. If the state of the given string is a winning state, then Alice wins, otherwise, Bob wins.
From each state, we can select two s and one in all possible ways and visit all reachable states from the current state and decide the current state winning or losing for the current player based on the above rules. For example, the state where , and , if we select two s from the pair and one from the pair , the new state will be , and as selecting two s from the pair will decrease by and selecting one from the pair will decrease by 1 and increase by at the same time.
For each string of length , there are at most different states. The complexity is .