We are to solve the hard version of "Palindrome Game (hard version)". The only difference: in easy version, the given string is initially a palindrome; in hard version, it is not guaranteed. But note that the problem statement for this particular problem (CF 1527B2?) Actually the problem states: "The only difference between the easy and hard versions is that the given string s in the easy version is initially a palindrome, this condition is not always true for the hard version."
So we are solving the hard version: the input string may or may not be a palindrome initially.
We need to decide outcome: ALICE, BOB, DRAW.
The game rules:
Goal: minimize one's own spent money? Actually "The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars." So it's like the total cost each player has incurred. They want to have a lower total cost than opponent at the end. They both play optimally.
This is a zero-sum game? Not exactly, because there is possibility of draw. But we can treat it as each player tries to maximize the difference (their savings) or minimize opponent's advantage.
We need to analyze optimal strategies.
First recall known solution for the easy version (when initial string is palindrome). Known result: For palindrome strings with zeros, the outcome depends on the number of zeros and especially the central character for odd length.
In easy version (palindrome initial string), the known results:
- If n is odd and the middle character is '0' (i.e., the palindrome has a central zero), then if there is exactly one zero (i.e., only the middle is zero), then Bob wins? Actually known: For palindrome, let cnt = number of zeros.
If n is odd and s[(n+1)/2] == '0' (center zero), then if cnt == 1 -> BOB? Wait, typical CF problem 1527B1: answer for palindrome: if zeros count == 1? Actually recall: "ALICE" usually wins except some cases where it's DRAW or BOB. Let's recall precisely.
The known results for the easy version (palindrome initial string) are:
- If the number of zeros is 0 (but problem says at least one zero) so ignore.
- If the number of zeros is 1: then BOB wins? Actually I think if zeros=1 and palindrome, then Bob wins because Alice must flip that zero (cost 1), then game ends. So Alice spends 1, Bob spends 0 -> Bob wins. So BOB.
- If zeros is odd? Let's recall from CF editorial: For palindrome, if n is odd and center is '0', then if zeros == 1 -> BOB; else ALICE wins? Actually I recall that for palindrome, if zeros is even, then BOB wins? Wait there's nuance: In palindrome version, typical solution: if zeros count is 1, BOB wins; else if zeros count is odd (and >1), then ALICE wins? I need to re-derive.
But this is for easy version; for hard version, the string may not be palindrome initially, so more possibilities.
We need to solve general case.
We'll analyze the game comprehensively.
Observations:
-
Flipping a '0' to '1' costs 1 for the player who does it.
-
Reverse costs nothing but changes the string order. The reverse operation can be used strategically to force opponent to flip a zero that would be more advantageous? Since flipping a zero at symmetric positions might have different effect? Actually since the game ends when all are '1', the positions matter only because the string may become palindrome or not, affecting ability to reverse and also the future costs.
-
Important: The reverse operation is allowed only if current string is NOT a palindrome and the previous move was NOT reverse. So you cannot reverse twice in a row. Also, after a reversal, the string might become palindrome? Possibly.
Goal: At the end, total cost each incurred. Players aim to minimize their own cost relative to opponent's, i.e., they want to have lower total cost. So it's a zero-sum game in terms of difference: Alice wants to have cost < Bob's cost; Bob wants to have cost < Alice's; if equal, draw.
Thus optimal play will be to maximize the difference (Bob's cost - Alice's cost) from Alice's perspective, and minimize from Bob's.
Let D = (Bob's total cost) - (Alice's total cost). At the end, if D > 0, Alice wins; D < 0, Bob wins; D = 0, draw.
Thus we can treat as Alice wants to maximize D, Bob wants to minimize D.
Now consider operations: Flipping a zero increases the player's own cost by 1. So if Alice flips, D decreases by 1 (since Alice's cost increases, so Bob's minus Alice's decreases). If Bob flips, D increases by 1. So flipping by Alice: D = D - 1; flipping by Bob: D = D + 1.
Reversal: costs 0, does not directly change D. But it changes the state and may affect future moves.
Thus reversal is a "free move" that can change the parity of whose turn it is? Actually it uses a turn: it's a move by a player, but no cost. So it's like a pass but with a string transformation and restrictions.
The reversal move can only be used if the string is not a palindrome and the previous move was not a reversal.
Thus reversal is like a tactical move to force opponent to flip in a disadvantageous way? Let's think.
Another key: The string consists of zeros and ones. The game ends when all ones. So total flips needed = number of zeros in the initial string (call totalZeros = cnt). But note that flipping a zero to one reduces the total zeros by 1. However, reversal does not change the multiset of characters; it only reorders them. So the total number of zeros remains constant until flipped.
Thus the game must end after exactly cnt flips (each flip changes a zero to one). So the number of flip moves is fixed: cnt flips will happen. But the order and who does them matters.
Additionally, there can be some reversal moves inserted between flips. So total moves >= cnt. The players can intersperse reversals as allowed.
Now, the outcome is determined by the parity of flips each does? If there were no reversals, the game would be just alternating flips starting with Alice. Since total flips = cnt. If both flip optimally (always some zero), then the number of flips each performs: Alice gets ceil(cnt/2), Bob gets floor(cnt/2). Costs: Alice spends ceil(cnt/2), Bob spends floor(cnt/2). So D = floor(cnt/2) - ceil(cnt/2) = - (cnt mod 2)?? Actually compute: if cnt even: Alice flips cnt/2, Bob flips cnt/2 => costs equal => D = 0 => draw. If cnt odd: Alice flips (cnt+1)/2, Bob flips (cnt-1)/2 => Alice spends one more => D = Bob's cost - Alice's cost = (cnt-1)/2 - (cnt+1)/2 = -1 => Bob wins. So without reversals, Bob would win when cnt odd, draw when even.
But reversals can change who flips which zeros and maybe skip flips? Actually they don't skip flips; but they can affect the ability to flip a particular zero? Since flipping any zero costs 1, there is no difference which zero you flip. However, flipping a zero can change whether the string is a palindrome, which then affects the ability to use reversal later. So the strategic value of reversals is that they can change the palindrome status, and also can swap the positions of zeros, potentially making the string become a palindrome or not, thus enabling/disabling reversal moves. Also reversal costs nothing, so it's a way to effectively pass the turn without spending money, but with the restriction that you cannot reverse if the string is palindrome or if previous move was reverse.
Thus a player may use reversal to avoid having to flip when it's their turn, especially if flipping would be disadvantageous. However, reversal may not always be available.
Thus we need to analyze the game as an impartial combinatorial game with cost structure.
The problem size up to n=1000, t up to 1000, but we need to compute outcome directly by analysis.
I recall that there is known solution for the hard version: It involves checking if the string is already a palindrome. If not, then usually Alice wins, except some special cases where it's a draw or Bob wins? Let's recall from Codeforces problem 1527B2 - Palindrome Game (hard version). I think I've seen solutions: For hard version, answer is:
- If the string is already a palindrome: answer is as in easy version (since it's palindrome).
- Else:
- Let mismatch = number of positions where s[i] != s[n+1-i] (i.e., number of pairs that are not equal). Since s is not palindrome, mismatch > 0.
- Also, count of zeros at symmetric positions? There might be a special case where there is exactly one mismatched pair and the total zeros is something? Actually the known solution: If the string is not a palindrome, then Alice always wins except when there is exactly one mismatched pair and the total number of zeros is odd? Wait, I need to think.
Better to recall editorial: For hard version, if s is already palindrome, use easy version logic. If not a palindrome:
Consider the number of positions i where s[i] != s[n+1-i] (i.e., the number of mismatched pairs, but each pair counts as two positions? Actually we consider pairs (i, n+1-i) for i < n+1-i. For each such pair, the two characters differ; one is '0' and the other is '1' (since mismatch means one is 0 and other is 1, because if both 0 they match? Actually both zeros are equal, so they match; both ones also match. So mismatch occurs exactly when one is 0 and the other is 1. So each mismatched pair contributes exactly one zero and one one.
Additionally, there may be central character if n odd; that is compared with itself; it's always matching because it's the same position. So central char contributes to zeros count if it's zero, but doesn't cause mismatch because it's palindrome condition automatically holds at center. So palindrome status depends on all symmetric pairs matching.
Thus for a non-palindrome, there is at least one mismatched pair. In such a pair, exactly one zero exists. So total zeros = zeros in mismatched pairs + zeros in matched pairs (both zeros) + possibly zero at center.
Now, the game: Since the string is not palindrome, the reverse operation is allowed (provided previous move not reverse). So Alice might start by reversing (cost 0). That could be advantageous.
Let's analyze the game from initial state when s is not palindrome.
Key insight: Because reverse is free and can be done when string is not palindrome, the player who can force the opponent to be the one who has to flip zeros in a palindrome state might gain advantage.
We should think about the structure: There are two types of zeros:
- "good" zeros that are in symmetric positions that are both 0? Actually a zero that is part of a matched pair (both characters are 0) will become 1 only when both are flipped? But flipping one zero in a matched pair changes that pair into a mismatched pair (since after flip, that position becomes 1, other remains 0). That then creates a mismatch, making the string not palindrome. Conversely, flipping a zero in a mismatched pair (where the other is 1) will make both become 1? Actually if we have a mismatched pair: (0,1). If we flip the 0 to 1, then both become 1, so the pair becomes matched (both ones). That removes the mismatch. So flipping a zero in a mismatched pair eliminates a mismatch, potentially making the string closer to palindrome.
Thus flipping zeros in mismatched pairs reduces the number of mismatches, while flipping zeros in matched pairs (both zeros) creates a mismatch.
The reversal operation swaps the entire string, which essentially swaps the positions in each symmetric pair? Actually reversal maps position i to n+1-i. So after reversal, the state of each symmetric pair is just swapped. For palindrome status, it's symmetric: after reversal, the string is reversed; but if the original string is not palindrome, the reversed string is the mirror image; it might still be not palindrome. In fact, the set of mismatched pairs remains the same? Let's examine: If we have a mismatched pair (i, j) with s[i]=0, s[j]=1. After reversal, the string becomes rev(s). At position i now we have original s[n+1-i] = s[j] = 1, and at position j we have original s[i] = 0. So the pair is still mismatched, just swapped. So the set of mismatched pairs (as unordered pairs) remains unchanged. Also, matched pairs stay matched. So reversal does not change the palindrome status; it simply reflects the string. Indeed, s is palindrome iff s = rev(s). So rev(s) may or may not equal s. If s is not palindrome, rev(s) is also not palindrome and in fact rev(s) != s. But after reversal, the string becomes rev(s). Is rev(s) palindrome? Not necessarily; but if we reverse again, we get back s. However, rev(s) is not necessarily palindrome. Actually if we reverse rev(s), we get s. So rev(s) is palindrome only if s = rev(rev(s)) = s, so rev(s) is palindrome exactly when s is palindrome. So reversal does not change the property of being palindrome; it toggles between s and rev(s) if they are distinct. So the string after reversal is still not palindrome if originally not palindrome. However, note: if we flip a zero and then the string becomes palindrome, then reversal becomes disallowed until perhaps later when it becomes non-palindrome again.
So the ability to reverse is present only when current string is not palindrome. So when the string becomes palindrome, no one can reverse until a flip makes it non-palindrome again.
Thus the game involves states: palindrome vs non-palindrome, and also whether previous move was reverse (to prevent consecutive reversals). Since reversal is free, players would like to use it to effectively skip a turn without cost, but they can't use it if the string is palindrome. Also, they can't use it if the previous move was reverse.
Thus the game resembles: On a non-palindrome state, a player may either flip a zero (cost 1) or reverse (cost 0). However, after a reversal, the opponent cannot reverse next turn, but can flip. After a flip, the opponent may reverse if the new string is not palindrome and the previous move was not reverse (since flip is not reverse, so reversal allowed). So reversal can be played alternatingly.
If the string is palindrome, only flip moves are possible.
Thus the optimal strategy often involves using reversal to force the opponent to be the one who flips the last zero or something.
We need to analyze the game outcome.
Let's denote:
- cnt0 = total number of zeros.
- Let mismatched pairs count = m (number of i < n+1-i such that s[i] != s[n+1-i]). Since each such pair contains exactly one zero and one one, the number of zeros in mismatched pairs = m (each mismatched pair contributes exactly one zero). Because if s[i] != s[n+1-i], one is '0' and other is '1'. So m zeros are in mismatched positions.
- Let matched pairs of zeros: pairs where both are '0'. Let's denote p = number of such pairs (each pair contributes 2 zeros). So zeros from matched zero pairs = 2p.
- For odd n, there is a middle character. If the middle char is '0', let's denote center_zero = 1, else 0.
Thus total zeros: cnt0 = m + 2p + center_zero.
Now, the string is palindrome if and only if m = 0.
So initial state: if m == 0 (palindrome), we need to apply easy version logic.
If m > 0 (non-palindrome), we need to analyze.
We'll first recall the known results from CF editorial for B2 (hard). I will try to reconstruct.
I recall that for non-palindrome, typically Alice wins, except when there is exactly one mismatched pair (i.e., m = 1) and the total number of zeros is something? Actually there is a special case where the answer is DRAW. Let me search memory: There is a CF problem "Palindrome Game (hard version)" where answer could be DRAW when there is only one '0'? No, because initial string not palindrome must have at least one mismatched pair, which includes at least one zero. Could be that there is exactly one zero? But if exactly one zero and string not palindrome, then there must be at least one mismatched pair; but if only one zero, then all other characters are '1'. For it to be non-palindrome, that zero must be in a position that is not symmetric? Actually if there is exactly one zero and the string is not palindrome, then the zero's symmetric counterpart must be '1' (since else if zero at symmetric position both zeros would be two zeros). So that is a mismatched pair. Then m=1 and p=0, center_zero maybe 0 if n odd? Possibly if n odd and zero at center, then it's palindrome because center matches itself, so not non-palindrome. So with one zero and non-palindrome, it must be that n even? Could be odd but zero not at center? For odd n, center is a single position; if that is zero, the string could still be non-palindrome if other mismatches exist. But if only one zero total, and it's at center, then all other are ones, and that would be palindrome because symmetric pairs are all ones? Let's test n=3, s=010? That is palindrome? 0-? positions: s[1]=0, s[3]=0? Actually s=010: indices 1,2,3: s1=0, s2=1, s3=0 -> palindrome because s1==s3. So it is palindrome. So if only one zero at center of odd length, palindrome. So for non-palindrome with only one zero, that zero must be in a non-central position, thus there is a mismatched pair: zero and its symmetric counterpart is 1, so m=1. Also no other zeros. So cnt0=1. So what is the outcome for such case? Let's try to simulate small examples to deduce.
Consider n=2, s="01". This is not palindrome (since "01" reversed "10"). cnt0=1, m=1 (only pair (1,2): 0 and 1), p=0, center_zero=0. So what is outcome? Let's think.
Game: initial s="01", not palindrome, Alice's turn.
Possible moves for Alice:
- Flip a zero: choose the only zero at position1 (or maybe both positions? only one zero). Flip it to '1': then string becomes "11". Game ends. Alice pays 1, Bob pays 0 => Alice loses (Bob wins) because Alice spent 1, Bob 0. So if Alice flips, she loses.
- Reverse: allowed because not palindrome and previous move not reverse. Reverse the string: becomes "10". Pay 0. Now Bob's turn with s="10", still not palindrome? "10" reversed is "01", not palindrome. Also previous move was reverse, so Bob cannot reverse (since reverse not allowed if last move was reverse). So Bob's options: flip a zero (the only zero at position2 now) -> becomes "11", Bob pays 1. Game ends. Total: Alice 0, Bob 1 => Alice wins. So if Alice reverses, she wins. So Alice will choose reverse and win. So outcome: ALICE.
Thus for (n=2, "01") => ALICE. So for m=1, cnt0=1, Alice wins.
Consider n=3, s="001"? Check palindrome? "001" reversed is "100", not palindrome. cnt0=2? Actually s=001: zeros at positions1 and2? Wait "0 0 1": zeros two, ones one. But is it palindrome? s1=0, s3=1 -> mismatch => not palindrome. So m? pairs: (1,3): 0 vs 1 -> mismatched. (2,2) center: 0. So mismatched pairs: m=1 (only pair (1,3) is mismatched because one 0, one 1). p (matched zero pairs) =0 because only pair (1,3) is mismatched. center_zero=1. So cnt0 = m + center_zero = 1 + 1 = 2. So m=1, cnt0=2.
Let's simulate to see outcome.
Initial: s="001", not palindrome. Alice's options:
- Reverse: becomes "100". Reverse is allowed. After reverse, s="100". Then Bob's turn. Now s="100", not palindrome (since reverse of "100" is "001", not equal). Also last move was reverse, so Bob cannot reverse. Bob's options: flip a zero. There are two zeros? Actually "100": zeros at positions2 and3? Wait "1 0 0": zeros at positions2 and3. But careful: The string after reversal: original "001" reversed is "100". So zeros: positions2 and3. But note: flipping any zero costs 1. But Bob wants to minimize his cost relative to Alice. Let's analyze outcomes.
If Bob flips a zero: which zero? Two zeros: one at pos2, one at pos3. Flipping either changes string. But we need to consider that flipping might affect palindrome status. Let's explore.
Case Bob flips the zero at position2: becomes "110". Now s="110". Check palindrome? "110" reversed "011": not palindrome. But game ends? Not yet because there is still a zero at position3? Actually "110" has a zero at position3? "1 1 0": yes zero at pos3. So not all ones. So game continues. After Bob's flip, Alice's turn with s="110". s="110" not palindrome? Check: s1=1, s3=0 -> not palindrome. Also last move was flip, so Alice can reverse (since not palindrome and previous move not reverse). Let's continue.
Case Bob flips the zero at position3: becomes "101". Now s="101". That's palindrome? "101" reversed is "101", yes palindrome. So after Bob's flip, string becomes palindrome "101", with one zero left at center? Actually "101": zeros? It has '0' at position2. So one zero left. It's palindrome. Alice's turn, with palindrome and previous move was flip, so Alice cannot reverse (since string is palindrome? Actually reverse is allowed only if string not palindrome. So reverse not allowed). So only flip is possible. Alice must flip the zero (cost 1). Then string becomes "111". Game ends. Totals: Alice: 0 (from initial reverse) + 1 = 1; Bob: 1 (from flip). So Alice spent 1, Bob spent 1 -> draw. But could Alice have done better? She cannot reverse because string palindrome, so forced flip. So outcome draw.
But maybe Bob would choose the other flip to get a better outcome for himself (Bob wants to win or at least avoid Alice winning). Let's compute both.
Option 1: Bob flips pos2 -> "110". Then Alice's turn.
Now s="110", not palindrome, last move flip so Alice can reverse (cost 0) or flip a zero.
-
If Alice reverses: becomes "011". Then Bob's turn. s="011", not palindrome, last move reverse so Bob cannot reverse. Bob must flip a zero (only zero at pos1? "0 1 1": zero at pos1). Flip -> "111", Bob pays 1. End totals: Alice: 0 (initial reverse) + 0 (her reverse) = 0; Bob: 1 (first flip) + 1 (second flip) = 2? Wait careful: Bob already flipped once on his turn (cost 1). Then after Alice reverse, Bob flips again cost 1. So total Bob = 2, Alice = 0. Alice wins (since 0 < 2). But check: Bob's first flip cost 1, then after Alice's reverse, Bob flips again cost 1, total Bob =2. So Alice wins.
-
If Alice instead flips a zero in "110": She could flip pos3 (the only zero) -> becomes "111". Then Alice pays 1. Totals: Alice: 0+1=1; Bob:1. Alice wins (1<1? Actually 1==1? Wait Alice 1, Bob 1 => draw? Bob's total =1, Alice=1 => draw. Actually Bob's total is 1 from his flip, Alice 1 from her flip, so equal -> draw. But Alice could achieve win by reversing. So Alice will choose reverse. So after Bob's flip to "110", Alice can force a win by reversing.
Thus Bob would not choose flip leading to "110" because Alice can force win. So Bob would choose flip leading to "101" which yields draw. So optimal Bob leads to draw.
Thus from initial reverse by Alice, outcome is draw. But could Alice have done something else initially besides reverse? She could flip a zero initially. Let's check.
If Alice flips a zero initially (instead of reverse). Options: s="001" has zeros at positions1,2,3? Actually positions: 1:'0',2:'0',3:'1'. So two zeros. She can flip one.
Case Alice flips pos1: becomes "101". That is palindrome (as we saw). Now Bob's turn with s="101", palindrome. Bob must flip (cannot reverse because palindrome). Flip the only zero (center) -> "111". Bob pays 1. Totals: Alice:1, Bob:1 -> draw.
Case Alice flips pos2: becomes "011". s="011" not palindrome? Check: "011" reversed "110", not equal. So not palindrome. Also last move was flip, so Bob can reverse or flip. Bob's turn.
Now state: s="011", zeros at pos1? Actually "0 1 1": one zero at pos1. Bob's options:
- Reverse: becomes "110". Then Alice's turn with s="110", not palindrome. Last move reverse so Alice cannot reverse. Alice must flip a zero (only zero at pos3? Actually "110": zeros at pos3? Wait "1 1 0": zero at pos3). Flip -> "111", Alice pays 1. Totals: Alice:1 (initial flip) +1 =2? Actually careful: Alice initially flipped (cost 1). Then Bob reversed (cost 0). Then Alice flips (cost 1). So total Alice = 2, Bob = 0. Bob wins (since 0<2). So if Bob reverses, he wins.
- Flip: Bob could flip the zero at pos1: becomes "111". Then Bob pays 1. Totals: Alice:1, Bob:1 -> draw. So Bob would choose reverse to win. So Bob wins in this line.
Thus if Alice flips pos2, she loses (Bob wins). So she would avoid that. Could she flip pos3? pos3 is '1', not zero. So only zeros at pos1 and pos2.
Thus Alice's options: reverse leads to draw? Actually we found reverse leads to draw (if Bob optimal) because Bob can force draw? Let's double-check reverse line thoroughly to see if Alice has any alternative after reverse that could lead to win despite Bob's optimal play.
We assumed after Alice reverse -> "100". Bob's best response gave draw. But could Alice, after Bob's response, do something else that yields win? We computed that if Bob flips to "101", the result is draw. If Bob flips to "110", Alice can win. Bob would choose "101" to force draw. So indeed Bob can force draw. So reverse leads to draw.
If Alice flips pos1, we got draw (both pay 1). So that's also draw. If Alice flips pos2, she loses. So best for Alice is draw. So outcome for s="001" is DRAW? Let's check if there's any other move: Could Alice reverse and then later maybe flip instead of something? But we considered optimal play after reverse. So seems draw.
But is it indeed draw? Let's check by exhaustive reasoning: Initial "001". If Alice reverses to "100". Then Bob's move: we considered two flips. But also, could Bob do a reversal? No, because previous move was reverse, so Bob cannot reverse. So only flips. So Bob's two possible flips: flip pos2 (zero) leading to "110", or flip pos3 (zero) leading to "101". Bob will choose the one that gives him best outcome (i.e., minimize D = Bob cost - Alice cost). Starting D=0 (since no costs yet). After Alice's reverse (cost 0), D remains 0. Bob's flip costs 1 if he flips, D increases by 1? Wait D = Bob cost - Alice cost. Initially both 0. After Alice's reverse, no cost, still 0. After Bob's flip, Bob cost increases by 1 => D becomes +1. Then game continues. Bob wants to minimize D (since he wants Bob's cost - Alice cost negative? Actually Bob wants his own cost less than Alice's, i.e., Bob cost < Alice cost => Bob cost - Alice cost < 0 => D < 0. So Bob wants D as low as possible (negative). Alice wants D as high as possible (positive). So after his flip, D=+1, which is good for Alice. But he can later influence. So he chooses flip that leads to eventual D lower.
After Bob flips to "101", D=+1. Now Alice's turn. String "101" palindrome. Only flip possible. She flips the zero (center) cost 1 => Alice cost increases by 1, so D becomes +1 -1 = 0. Game ends. Final D=0 => draw.
After Bob flips to "110", D=+1. Now Alice's turn with "110" not palindrome, last move flip so she can reverse or flip. If she flips, she pays 1 => D becomes 0 (Bob cost 1, Alice 1) => draw. If she reverses, she pays 0 => D stays +1, and then Bob must flip (since can't reverse) the last zero => Bob pays another 1 => D becomes +2. Final D=+2 => Alice wins. So Alice would reverse to get win. So Bob prefers the "101" flip which forces draw. So draw.
Thus from reverse, result draw.
Now, can Alice do something else initial? She could do nothing else. So best Alice can get is draw? So outcome is DRAW? But the sample for hard version? The problem statement shows an example input:
3
3
110
2
00
4
1010
Outputs: ALICE, BOB, ALICE
We see for "110" (first test case) output ALICE. That's palindrome? "110" reversed "011", not palindrome. Actually "110" is not palindrome. But output says ALICE. Wait but sample says "3 110" output ALICE. But we earlier analyzed "001" which is similar but with zeros at positions? "110" has zeros? "110" has one zero? Actually "110": positions:1='1',2='1',3='0' -> zero at position3. That's palindrome? Check: s1=1, s3=0 => not palindrome. So non-palindrome. Total zeros cnt0=1, m? For "110", pairs: (1,3): 1 vs 0 => mismatched, so m=1; center? n=3 odd, center position2 is '1' so center_zero=0. So cnt0=m=1. That's same as "01" case but n=3. We earlier simulated "01" (n=2) and got ALICE. For n=3 with cnt0=1, m=1, it should also be ALICE? Let's simulate "110" quickly.
Initial s="110", not palindrome. Alice can reverse? Yes. Reverse becomes "011". Now Bob's turn. s="011", not palindrome, previous move reverse so Bob cannot reverse. Bob must flip a zero. "011" zeros? Actually "0 1 1": zero at pos1. Flip it -> "111", Bob pays 1. Game ends. Totals: Alice 0, Bob 1 => Alice wins. So ALICE. So for cnt0=1, m=1, Alice wins (by reversing). That matches our earlier conclusion for "01". So for m=1 and cnt0=1, ALICE.
Now for "001", cnt0=2, m=1, we got DRAW. That suggests that when m=1 and cnt0=2 (with center_zero=1), the result might be DRAW.
What about other combinations? Let's test s="0100"? Not palindrome? "0100" reversed "0010", not palindrome. Let's compute parameters: n=4 even. pairs: (1,4): 0 vs 0? Actually s1=0, s4=0 -> matched (both 0). (2,3): 1 vs 0? s2=1, s3=0 -> mismatched. So m=1 (one mismatched pair), p=1 (one matched pair of zeros at (1,4)), center_zero=0. cnt0 = m + 2p = 1+2=3. So m=1, cnt0=3. What's outcome? Likely ALICE? Need to simulate.
But maybe known result: For non-palindrome, if m = 1 and total zeros is 2? Actually our case gave DRAW. If m = 1 and total zeros is 1 -> ALICE. If m >= 2? Possibly ALICE always wins. And if m = 0 (palindrome), use easy version logic. Also perhaps there is a case where BOB wins for non-palindrome? Could be if the string is palindrome originally? Actually if palindrome, Bob may win in certain cases. But non-palindrome, seems Alice at least doesn't lose, maybe draws in some edge case. However, recall from known solutions: In hard version, if string is not palindrome, the answer is "ALICE" except when there is exactly one zero in the string? But we saw "001" (2 zeros) gave draw. Wait is "001" considered non-palindrome? Yes. So draw exists.
Actually I've seen some CF solutions: For hard version, if the string is already palindrome, answer is as easy version: if number of zeros is odd and greater than 1, Alice wins? Or maybe: if zeros count is even: Bob wins? Let's recollect.
Let's search memory: I recall reading editorial: For palindrome (easy) the answer is:
- If number of zeros is even: BOB wins.
- If number of zeros is odd: ALICE wins, except when zeros = 1 then BOB? Actually check: For palindrome, zeros count = z. If z is even: then Bob wins? Let's test simple: n=1, s="0" (palindrome, zeros=1). Who wins? Alice must flip the only zero (cost 1) -> Alice 1, Bob 0 => Bob wins. So for z=1, Bob wins. If z=2, palindrome: e.g., "00". This is palindrome (even zeros). Let's simulate: n=2, s="00". Alice's turn. Only flips possible (since palindrome). She flips one zero -> string becomes "10"? Actually flipping a zero yields "10". Now string is not palindrome? "10" vs "01" not palindrome. After Alice flip, Bob can reverse? Since string not palindrome and last move not reverse (flip), Bob can reverse. Let's simulate: start "00", zeros=2. Alice flips first zero (cost 1) -> "10". Now Bob can reverse (cost 0) -> "01". Then Alice's turn: now "01" has one zero. She could flip it? But flipping would end game with cost 1 for her. Or she could reverse? But "01" is not palindrome, and last move was reverse, so she cannot reverse. So she must flip (cost 1). Then game ends. Totals: Alice 1+1=2, Bob 0 => Bob wins. Could Alice have flipped the other zero? symmetric. So outcome Bob wins. So for palindrome with z=2, Bob wins. For z=3? e.g., "000"? Actually palindrome "000": n=3. Simulate? But "000" is palindrome; zeros=3. Alice flips? She could flip center? Let's see. If she flips center, becomes "010". Now string palindrome? "010" is palindrome. Bob's turn, must flip. He flips one of the zeros? But maybe there is strategy. Let's simulate optimally. According to known results, for palindrome with odd zeros > 1, Alice wins. Let's test "000". Options: Alice flips center (cost 1) -> "010". Now Bob's turn, string palindrome with zeros=2 (two zeros at ends). As we saw, with palindrome of 2 zeros, Bob will win if he plays optimally? But Bob is now the player to move in a palindrome with 2 zeros. In the easy version, with palindrome and zeros=2, the player to move? Wait we need to consider who is moving. In our simulation for "00" (2 zeros) with Alice moving, Bob won. So if it's Bob's turn on palindrome with 2 zeros, then the player to move (Bob) would be in the position that Alice was in "00"? Actually we need to compute outcome for a given state with current player. For palindrome, the outcome depends on number of zeros and who moves. Since the game is symmetric except for the fact that Alice moves first overall, but within subgames, the roles reverse. So we need to determine the outcome from a given position with player to move.
But the known easy version result for initial palindrome (Alice to move) is:
- If zeros is 1 => BOB wins.
- If zeros is odd (and >1) => ALICE wins.
- If zeros is even => BOB wins.
But is that correct? Let's test zeros=3 palindrome (e.g., "000"): According to that, odd >1 => ALICE wins. Simulate to see.
Game: "000" (palindrome). Alice's options:
- She can flip a zero. She could flip an end or center. Let's analyze:
Case 1: Flip an end (position1). Cost 1. New string: "100". This is not palindrome (since "100" reversed "001"). Now Bob's turn with "100", non-palindrome. Bob can reverse? Since previous move was flip, and string not palindrome, he can reverse. Let's consider optimal.
After Alice flips end -> "100". zeros left = 2 (positions2 and3 are zeros? Actually "1 0 0": zeros at pos2,3). Bob can either flip or reverse.
If Bob reverses: cost 0, string becomes "001". Then Alice's turn with "001" (non-palindrome). As we analyzed earlier for "001", we found with Alice to move, the outcome is? We analyzed "001" as initial state with Alice to move? Actually we analyzed "001" with Alice to move, and we found it resulted in draw? But that was with optimal play? Let's re-evaluate "001" as a starting position (non-palindrome) with Alice to move. That analysis gave draw? We got draw for "001" when Alice moved first. But in this subgame, after Bob reverses, it's Alice's turn with "001". According to our analysis, from "001" with Alice to move, the best outcome was draw. So if Bob does that, final outcome would be draw (if optimal from there leads to draw). However, Bob might have better move.
If Bob flips a zero: He can flip either zero. He could flip the zero at position2 to get "110", or position3 to get "101". Let's compute outcomes.
We need to compute from state "100" with Bob to move, with current costs: Alice 1, Bob 0. D = Bob - Alice = -1 (since Bob 0, Alice 1). Bob wants D negative? Actually Bob wants his own cost less than Alice's => D < 0. Currently D = -1, which is favorable to Bob (since negative means Bob has paid less). But he may want to keep it negative. He can choose moves.
We'll simulate with full game tree.
But perhaps known result is different. Anyway, we should derive systematically.
We need to build a theoretical solution for the general case.
I recall from Codeforces problem 1527B2, the solution is:
- If the string is already palindrome, then the answer is:
- If cnt0 == 1: BOB
- else if cnt0 % 2 == 0: BOB
- else: ALICE
But this is for the easy version? Actually easy version (B1) answer is:
if (cnt0 == 1) Bob
else if (cnt0 % 2 == 0) Bob
else Alice.
But I've also seen in editorial that for palindrome with cnt0 even, Bob wins; with cnt0 odd, Alice wins, except when cnt0 = 1 where Bob wins (but 1 is odd so it's exception). So they say: if cnt0 is even: BOB; if cnt0 is odd and >1: ALICE; if cnt0 == 1: BOB.
But that is for palindrome? Let's test with cnt0=3 (odd >1) we expect ALICE. Let's test "000". Simulate more carefully to verify.
Game: "000" (palindrome, 3 zeros, n=3, Alice to move). We'll analyze with optimal play.
State representation includes string and whether last move was reverse (but initially last move not reverse). Since string is palindrome, only flips allowed.
But after a flip, string may become non-palindrome, then reversal becomes possible.
We need to compute the outcome value (the difference D) under optimal play. Let's do a thorough analysis of palindrome states.
But before that, we need a consistent method for the hard version.
I think the known solution for hard version is:
Let cnt0 = number of zeros.
Let mismatch = number of i with s[i] != s[n+1-i] (i.e., count of positions where characters differ). But since each mismatched pair contributes 2 positions but they are correlated. Actually each such pair is two positions, but it's easier to consider the number of mismatched pairs, let's call diff = number of indices i < n+1-i such that s[i] != s[n+1-i]. So diff = m.
If diff == 0 (palindrome): then as per easy version:
if cnt0 == 1: BOB
else if cnt0 % 2 == 0: BOB
else: ALICE
If diff > 0:
if diff == 1 and cnt0 == 2: DRAW? Actually from our example "001" (diff=1, cnt0=2) we got DRAW. But need to verify for other cases.
Also we saw diff=1, cnt0=1 => ALICE.
diff=1, cnt0=2 => DRAW.
What about diff=1, cnt0=3? Let's test.
Example: n=4, s="0100" (diff=1, cnt0=3). Let's simulate to see outcome.
We'll simulate with optimal play. We'll have to track the state and try to determine result.
We need to determine from state (string, player to move, last move was reverse? and costs). Since n up to 1000, we cannot do DP for all states. But we can reason combinatorially.
Observation: The only "interesting" aspect is the ability to reverse when the string is not palindrome. Since reversal costs nothing and doesn't change the set of zeros or the pattern of matched/mismatched pairs? Actually reversal swaps the positions, but the multiset of characters per symmetric pair is unchanged: for each symmetric pair (i, j), after reversal, the values at i and j swap. So the property of whether a pair is matched (both same) or mismatched (one zero, one one) stays the same, but the specific assignment may flip. So from the perspective of the game, the exact positions may matter because after a flip, the distribution changes. However, we can think in terms of the counts: number of matched zero pairs (both zeros), number of mismatched pairs, and whether there is a central zero. Because these determine the possibilities.
Moreover, flipping a zero in a matched zero pair converts it to a mismatched pair (since one becomes 1, the other remains 0). Flipping a zero in a mismatched pair converts it to a matched ones pair (both become 1) — that reduces the number of zeros by 1 and reduces the number of mismatched pairs by 1 (since the pair becomes matched (both ones)). Flipping a central zero (if odd length) reduces zeros by 1 and reduces cnt0; also the central is always matched with itself, so flipping it removes that zero without affecting mismatch count.
Reversal does not change counts, but it might change which player can reverse next because after reversal, the previous move is reverse, so the opponent cannot reverse next turn. Also, after reversal, the string is still non-palindrome if diff > 0.
Also, after a flip, the string may become palindrome. That happens when after the flip, all symmetric pairs become matched. When does that occur? Initially diff > 0. If we flip a zero that is part of a mismatched pair, then that pair becomes matched (both ones), so diff reduces by 1. If after that flip, diff becomes 0 and there is no central zero? But central zero doesn't affect palindrome if it's zero? Actually central character doesn't need to match anyone, it's always fine. So after flipping a zero in a mismatched pair, diff reduces by 1. If originally diff == 1, flipping that mismatched zero will make diff = 0, and the string becomes palindrome (since all pairs match, central may be zero or one, but central zero does not break palindrome). So for diff == 1, flipping the zero in the unique mismatched pair will make the string palindrome. If we flip a zero from a matched zero pair, then that pair becomes mismatched (since one becomes 1, the other remains 0), so diff increases by 1. So flipping matched zero increases diff.
Thus, diff can change by ±1 depending on which type of zero is flipped, except flipping central zero doesn't change diff.
Now, note that reversal is only allowed when diff > 0 (string not palindrome) and last move not reverse. So when diff == 0, reversal is not allowed, only flips.
Thus, the game state can be characterized by (diff, p, center_zero, lastMoveWasReverse?, player to move). But maybe we can simplify by noting that optimal strategies often revolve around the number of zeros and the mismatches.
Observation: Reversal is a free move that effectively allows a player to skip their turn without spending money, but with the cost that it may change who flips next and the status. However, because reversal does not change the configuration of matched/mismatched pairs counts (just swaps sides), the subsequent flip options are symmetric. So from a strategic standpoint, the only effect of reversal is to pass the turn (since it costs 0) but also impose that the opponent cannot reverse next (since last move was reverse). This can be crucial.
Also, note that if diff > 0, the player who is to move can choose to reverse, provided the previous move was not reverse. If they reverse, they effectively pass (cost 0) and the opponent cannot reverse next turn. So if diff > 0 and the previous move was not reverse, the player can force the opponent to be in a position where reversal is not allowed, thus the opponent may be forced to flip (if they want to avoid something) or perhaps they still have a move of flipping.
But the opponent could also choose to flip, which might change diff.
Thus, we have a game that can be analyzed as a normal play game with costs.
Maybe we can think in terms of "passing" opportunities.
Key Idea: In many such games, if the string is not a palindrome, Alice can always force a win except some edge cases where it's a draw. Because she can use the reverse move strategically.
From memory of CF editorial for B2, the solution was:
Let cnt0 = total number of zeros.
Let diff = number of positions i with s[i] != s[n+1-i] (i.e., count of mismatched positions). Actually they define diff as the number of indices where s[i] != s[n+1-i] (for i from 1 to n). That count includes both positions in a mismatched pair, so it's 2*m (since each mismatched pair contributes two indices). But sometimes they use diff as the number of mismatched pairs? Need to recall.
Let me check known solutions from CF: I think they compute:
int diff = 0;
for(int i = 0; i < n/2; i++) if(s[i] != s[n-1-i]) diff++;
Then they do:
if(diff == 0) {
// palindrome case
if(cnt0 == 1) cout << "BOB\n";
else if(cnt0 % 2 == 0) cout << "BOB\n";
else cout << "ALICE\n";
}
else {
if(diff == 1 && cnt0 == 2) cout << "DRAW\n";
else cout << "ALICE\n";
}
But is that accurate? Let's test with our examples:
Example "001": n=3, s="001". For i=0 to floor(2/2)=1? Actually n/2 integer division: 3/2=1. So compare i=0: s[0]='0', s[2]='1' -> diff++ => diff=1. cnt0=2. So diff==1 and cnt0==2 => DRAW. That matches our simulation.
Example "110": s="110": compare i=0: s[0]='1', s[2]='0' -> diff=1; cnt0=1. So diff==1 and cnt0==2? No, cnt0=1 not 2, so condition false, thus output ALICE. That matches.
Example "0100": n=4, compare i=0: s[0]='0', s[3]='0' -> equal; i=1: s[1]='1', s[2]='0' -> diff=1. cnt0=3. So diff==1 and cnt0==2? false, so output ALICE. We haven't simulated this but likely ALICE.
Example "1010": from sample: n=4, s="1010". Let's compute: s[0]='1', s[3]='0' -> diff? 1 vs 0 => diff=1; s[1]='0', s[2]='1' -> diff=2 (since also differ). Actually both pairs mismatched? Let's see: indices 0-3: 1 vs 0 -> mismatch; indices 1-2: 0 vs 1 -> mismatch. So diff=2. cnt0 = number of zeros: s=1 0 1 0 => zeros at positions1 and3 => cnt0=2. So diff=2, cnt0=2. According to above rule: else (since diff>0 and not (diff==1 && cnt0==2)) => ALICE. Sample output says ALICE. So matches.
Now test "00": palindrome case (diff=0). cnt0=2, even. According to palindrome rule: even => BOB. Sample output for "00" is BOB. Indeed second test case: "00" output BOB. That matches.
First test case "110" output ALICE, matches.
Thus the rule seems consistent.
But we need to verify more edge cases, particularly when diff > 0 and diff == 1 and cnt0 == 2 leads to DRAW. Are there any other draws? Possibly when diff == 0 (palindrome) there can be draws? In easy version, I recall there is no draw. In sample, palindrome "00" leads to BOB, not draw. So draws only in that special non-palindrome case.
But is that the complete rule? Let's test a case with diff == 1 and cnt0 == 2 but with center_zero? Already "001" is example. What about n=2, s="01" gave diff=1, cnt0=1 => ALICE. Good.
What about n=2, s="10" same.
Now consider n=4, s="0010"? Let's compute: s=0 0 1 0. pairs: (0,3): 0 vs 0 -> match; (1,2): 0 vs 1 -> mismatch. So diff=1. cnt0: zeros at positions0,1,3 => 3 zeros. So cnt0=3, diff=1 => ALICE.
Consider n=5, s="00100"? This string: 0 0 1 0 0. Check palindrome? It's symmetric? Let's compute: positions: 0-4: 0 vs 0 match; 1-3: 0 vs 0 match; center position2:1. So it's palindrome actually. Because s[0]=0, s[4]=0; s[1]=0, s[3]=0; s[2]=1. So it's palindrome. So diff=0. So not our case.
Consider n=5, s="00010"? That's