Stick Game
Dear Strategist,
You are observing a curious ring-shaped game played by Alice and Bob. Around a circle are exactly 24 upright sticks, equally spaced and numbered 0 through 23 clockwise. Each stick is permanently owned by exactly one of the two players: Alice owns some of them, Bob owns the rest. At the start all sticks are standing.
The players alternate turns, with Alice moving first. On a turn the current player must choose one of several patterns and a rotation of that pattern, then attempt to knock over every standing stick selected by that rotated pattern. Patterns may include sticks owned by either player; knocking them over is allowed. A pattern specifies a subset of the 24 relative positions (indexed 0..23). Rotating it by r (0 ≤ r < 24) means each chosen position i in the pattern refers to stick (i + r) mod 24.
Applying a chosen (pattern, rotation) pair simultaneously knocks over every still-standing stick at the selected positions after rotation. (Already knocked sticks stay down.) The outcome of the move is then evaluated in this precise order:
- If the move knocks over no new sticks (i.e. every targeted stick was already down), the current player immediately loses.
- Else, if after the move all of the opponent's sticks are now down (regardless of the state of the mover's own sticks), the current player immediately loses. (Delivering the final blow to the opponent backfires!)
- Else, if after the move all of the current player's own sticks are now down, the current player immediately wins.
- Otherwise the game continues and the turn passes to the other player.
Both players play optimally with the following priorities:
- First, they try to force a win; among all winning strategies they choose one that wins in the fewest possible turns.
- If they cannot force a win, they try to make the game last as many turns as possible (assuming the opponent also plays optimally).
- Tie-break (applied only among moves that preserve the best achievable game length for that player): the eventual winner chooses the smallest available (pattern_index, rotation) pair; the eventual loser chooses the largest. Pairs compare by pattern_index first, then rotation.
Your task is to determine under optimal play: (1) who wins, (2) the number of turns taken until the game ends (i.e. the number of moves actually executed), and (3) one concrete optimal sequence of moves. A sequence of moves is represented by ordered pairs (pattern_index, rotation). Pattern indices are 0-based in input order (0..P-1); rotations are integers 0..23.
Input Format
- Line 1: A 24-character string S consisting of letters 'A' and 'B'. Character i (0-indexed) gives the owner of stick i. S contains at least one 'A' and at least one 'B'.
- Line 2: An integer P: the number of patterns.
- Next P lines: Each line is a 24-character string of '0'/'1' where '1' marks positions included in the pattern before rotation (must contain at least one '1'). Position i corresponds to stick i before rotation.
Output Format
- Single line: winner T followed by T pairs pattern_index rotation (all space-separated). Winner is
AliceorBob. T is the number of turns executed. The first pair corresponds to Alice's first move.
So the line looks like: winner T p1 r1 p2 r2 ... pT rT
Notes & Clarifications
- You may assume optimal players will never intentionally choose a losing move when a winning move exists, nor shorten a forced loss when it can be prolonged.
- A move that would both knock over the opponent's last standing stick(s) and also your own last stick(s) is a loss for the mover (rule order: opponent-all-down checked before self-all-down win).
- When multiple moves lead to the same eventual outcome quality (fastest possible win in X turns or longest unavoidable loss in L turns), apply the local tie-break: future winner picks smallest (pattern_index, rotation), future loser picks largest.
- The initial ownership counts of sticks may differ (they need not be 12–12); all rules refer simply to "all of the opponent's sticks" or "all of your sticks" regardless of their counts.
- The turn count T is exactly the number of executed moves and matches the length of the listed sequence.
Example 1 Input
ABABABABABABABABABABABAB
2
110000000000000000000001
101000000000000000000010
Example 1 Output (One Optimal)
Alice 7 1 0 1 23 1 6 1 22 0 11 1 21 1 16
Example 2 Input
AAAAAAAAAAABABBBBBBBBBBB
2
111111111111000000000000
000000000001011111111111
Example 2 Output (One Optimal)
Bob 2 1 23 1 0
Good luck bringing the ring down — carefully!