Under construction

Still being built. Things may change

Shuffle Game

Interview

Time limit1.5sMemory limit1024 MB

Summary
Given deck X and two decks P1 and P2, find the maximum LCS length between X and any interleaving of P1 and P2.
Level

Medium6 of 10

Topics
Dynamic programming, String
Solved
No attempts yet

Problem

Shuffle Game is a simple card game between the dealer and the player. At the start, the same deck of nn cards is given to both the dealer and the player. Each card has a suit, one of C, D, H, or S, followed by a kind, one of 2, 3, 4, 5, 6, 7, 8, 9, 10, A, J, K, or Q. So there are 52 different cards, and the same card can appear more than once in a deck.

After the cards are dealt, the dealer builds their own deck XX from the dealt cards using any shuffling method and shows XX to the player. Then the player builds a deck YY as follows. YY starts empty.

  • Step 1. Build two decks P1P_1 and P2P_2 from the deck the player was given. The two decks can have different sizes.
  • Step 2. Interleave P1P_1 and P2P_2. That is, repeatedly move the bottom card of P1P_1 or the bottom card of P2P_2 onto the current top of YY, until both P1P_1 and P2P_2 are empty. The player does not have to alternate between P1P_1 and P2P_2. Both the dealer and the player build their decks from the same nn cards, so YY always contains the same cards as XX.

A deck is written as its sequence of cards from bottom to top. The player's score is the length of the longest common subsequence (LCS) of the sequences XX and YY.

For example, suppose the deck (C2, CJ, D5, HA, S7) with n=5n = 5 is given to both players. The dealer builds X=X = (CJ, D5, HA, C2, S7) and shows it to the player. The player builds P1=P_1 = (D5, HA) and P2=P_2 = (CJ, S7, C2), then interleaves them into Y=Y = (D5, CJ, S7, HA, C2). The score is 3, because (CJ, HA, C2) is a longest common subsequence of XX and YY. However, the player can interleave the same P1P_1 and P2P_2 into Y=Y = (CJ, D5, HA, S7, C2), which gives a score of 4.

Given nn, XX, P1P_1, and P2P_2, compute the maximum score the player can achieve after Step 2.

Input

The first line contains three positive integers nn, pp, and qq (3≤n≤5003 \le n \le 500, p+q=np + q = n), where nn is the number of cards in the initial deck, and pp and qq are the numbers of cards in P1P_1 and P2P_2. The next three lines contain the dealer's deck XX with nn cards, and the player's decks P1P_1 with pp cards and P2P_2 with qq cards. Each card is written as its suit (an uppercase letter C, D, H, or S) followed by its kind (2, 3, 4, 5, 6, 7, 8, 9, 10, or the uppercase letter A, J, K, or Q). The cards on each line are listed from the bottom to the top of the corresponding deck.

Output

Print one line containing the maximum score the player can achieve from XX, P1P_1, and P2P_2 after Step 2.

Examples3

  1. Example 1

    Input
    5 2 3
    CJ D5 HA C2 S7
    D5 HA
    CJ S7 C2
    
    Expected output
    4
    
  2. Example 2

    Input
    6 3 3
    C9 HK SQ SQ H2 CA
    CA HK SQ
    H2 C9 SQ
    
    Expected output
    4
    
  3. Example 3

    Input
    7 3 4
    S9 C10 DJ S6 S7 SA DQ
    DJ S6 S7
    S9 C10 SA DQ
    
    Expected output
    7