Snap

Time limit1sMemory limit128 MB

Summary
Simulate the two-player Snap card game with card flipping, pile recycling, and a fixed random tie-breaker for up to 1000 turns.
Level

Medium5 of 10

Topics
Simulation, Queue, Implementation
Solved
No attempts yet

Problem

Snap is a two-player card game. The deck contains several copies of each type of card. At the start, each player holds one half of the deck as a face-down pile, in some fixed order, and plays the cards one at a time from the top, laying each one face up onto a second pile. When a player's face-down pile is exhausted, that player's face-up pile is turned over to become the new face-down pile, and play continues.

The two players play in lockstep: on every turn both players reveal the top card of their face-down pile at exactly the same instant. If the two revealed cards are of the same type, both players shout "Snap!", and whoever shouts first takes the other player's entire face-up pile and places it, keeping its order, on top of their own face-up pile.

Play continues until one player holds every card; that player wins.

Simulate a game of Snap to decide whether it finishes within 1000 turns and, if so, who wins.

Input

The first line contains Jane's face-down pile, listed from top to bottom. The second line contains John's face-down pile, also from top to bottom. Each card type is a single letter or digit. Jane and John start with the same number of cards, at most 50 each.

Output

Which player shouts "Snap!" first is decided by a fixed pseudo-random number generator. Let x0=11x_0 = 11 and

xn=(1103515245⋅xn−1+12345) mod 231.x_n = (1103515245 \cdot x_{n-1} + 12345) \bmod 2^{31}.

Each time a "Snap!" is called, advance the generator once; the kk-th call uses the value xkx_k. If ⌊xk/141⌋\lfloor x_k / 141 \rfloor is even, Jane shouts first; otherwise John shouts first.

Every time Jane shouts first, print Snap! for Jane: followed by Jane's face-up pile from top to bottom (after she has taken John's pile). Every time John shouts first, print Snap! for John: followed by John's face-up pile from top to bottom (after he has taken Jane's pile). When the game ends, print Jane wins. or John wins., whichever applies. If the game has still not ended after each player has turned over 1000 cards, print Keeps going and going ....

Examples2

  1. Example 1

    Input
    ABCDA
    CBADC
    
    Expected output
    Snap! for Jane: BCBA
    Snap! for Jane: DADCBCBA
    Snap! for John: CBAC
    Snap! for John: ADADCBAC
    John wins.
    
  2. Example 2

    Input
    A
    A
    
    Expected output
    Snap! for Jane: AA
    Jane wins.