Under construction

Still being built. Things may change

Irrefutable Outcome

Time limit20sMemory limit1024 MB

Summary
Players alternately remove end pieces of their own color from an I/O row; output the winner and the winner's optimal score.
Level

Medium6 of 10

Topics
Game theory, Dynamic programming, String
Solved
No attempts yet

Problem

Izabella and Olga are playing a game alternating turns. Izabella plays first. The game starts with all game pieces arranged in a single row. The pieces come in two colors: indigo and orange. During Izabella's turns, she must choose and remove an indigo piece that is either the leftmost or rightmost piece remaining. During Olga's turns, she must choose and remove an orange piece that is either the leftmost or rightmost piece remaining. If at any point one of the players does not have a legal move (possibly because there are no pieces remaining), that player loses the game, and the other player is awarded 11 point plus 11 additional point for each piece that remains on the board.

We use an uppercase letter I to represent indigo pieces and an uppercase letter O to represent orange pieces. Suppose, for example, that they play with the following starting board: IOIOOOII.

On her first turn, Izabella can choose to remove either the leftmost or rightmost pieces, as both are indigo. Suppose she chooses the leftmost. Then, the board would become OIOOOII. Then, Olga would have no choice but to remove the new leftmost piece, as the rightmost piece is not orange, leaving IOOOII. Izabella can choose again, and this time she chooses the rightmost piece, leaving IOOOI for Olga's turn. At this point, Olga has no valid move, so Izabella won. Since there are 55 pieces remaining, Izabella wins 1+5=61+5=6 points in total.

Each player plays optimally trying to win and to maximize their own score. A player that cannot guarantee a win plays to minimize the opponent's score.

Given the starting board, can you find out who wins and what is their score?

Input

The first line of the input gives the number of test cases, TT. TT lines follow. Each line represents a test case and contains a string BB representing the status of the board. The ii-th character in BB is I if the ii-th piece from the left is indigo and O if it is orange.

Output

For each test case, output one line containing Case #x: y z, where xx is the test case number (starting from 1), yy is the initial of the winner (I for Izabella or O for Olga), and zz is the score the winner gets.

Limits

  • 1≤T≤1001 \le T \le 100.
  • Each character of BB is either an uppercase letter I or an uppercase letter O.

Hints

In Sample Case #1, Izabella can do better than the example in the statement. If she starts by removing the rightmost piece, Olga has no possible moves and Izabella wins with 77 pieces remaining. Izabella wins 1+7=81+7=8 points in total.

In Sample Case #2, Izabella cannot even make her first move, so Olga wins!

In Sample Case #3, neither player has a choice on what to play, and Olga wins after all pieces are exhausted, so she gets only 11 point.

In Sample Case #4, all pieces are exhausted at the end of the game too, but it is Izabella who comes out with the win.

Examples1

  1. Example 1

    Input
    5
    IOIOOOII
    OIIIIO
    IO
    IOIOIOI
    IOIOIOOIO
    
    Expected output
    Case #1: I 8
    Case #2: O 7
    Case #3: O 1
    Case #4: I 1
    Case #5: O 6