After each move, the number of ones in the string increases by 11. So the game is finite.

To solve the problem, let’s make n2\frac{n}{2} pairs of characters: (s1,sn),(s2,sn1),(s3,sn2),,(sn2,snn2+1)(s_1, s_n), (s_2, s_{n-1}), (s_3, s_{n-2}), …, (s_{\frac{n}{2}}, s_{n-\frac{n}{2}+1}). Each pair of characters should be equal to form a palindrome. Initially, there are 33 possible types of pairs: (0,0)(0, 0), (1,1)(1, 1) and (0,1)(0, 1) (or (1,0)(1, 0)). If nn is odd, there is a single character at the center of the string which is either 00 or 11. Let the count of each type of pairs be cnt00cnt_{00}, cnt11cnt_{11} and cnt01cnt_{01} respectively and midmid be the central character which is 00 or 11. The string become a palindrome when cnt01cnt_{01} is 00. There will be no valid move when cnt0<2cnt_0 < 2 or cnt1<1cnt_1 < 1 where cnt0cnt_0 and cnt1cnt_1 are number of zeros and ones in the string respectively.

cnt0=2×cnt00+cnt01cnt_0 = 2\times cnt_{00}+cnt_{01} when n is even.

cnt0=2×cnt00+cnt01+(1mid)cnt_0 = 2\times cnt_{00}+cnt_{01}+(1-mid) when n is odd.

cnt1=ncnt0cnt_1 = n-cnt_0

Now, the problem can be solved using dynamic programming where the states are cnt00cnt_{00}, cnt01cnt_{01} and midmid (if nn is odd). cnt11=n2cnt00cnt01cnt_{11} = \frac{n}{2}-cnt_{00}-cnt_{01} 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 00s and one 11 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 cnt00=3cnt_{00} = 3, cnt01=2cnt_{01} = 2 and mid=1mid=1, if we select two 00s from the pair (0,0)(0, 0) and one 11 from the pair (0,1)(0, 1), the new state will be cnt00=3cnt_{00}=3, cnt01=1cnt_{01}=1 and mid=1mid=1 as selecting two 00s from the pair (0,0)(0, 0) will decrease cnt00cnt_{00} by 11 and selecting one 11 from the pair (0,1)(0, 1) will decrease cnt01cnt_{01} by 1 and increase cnt00cnt_{00} by 11 at the same time.

For each string of length nn, there are at most 2×n2×n22\times\frac{n}{2}\times\frac{n}{2} different states. The complexity is O(n2)O(n^2).

Statistics

25% Solution Ratio
faria_efaEarliest, Feb '22
faria_efaFastest, 0.1s
nusuBotLightest, 393 MB
faria_efaShortest, 2455B
Toph uses cookies. By continuing you agree to our Cookie Policy.