#P14732. [ICPC 2022 Seoul R] Shuffle Game

[ICPC 2022 Seoul R] Shuffle Game

题目描述

Shuffle Game is a simple card game between the dealer and the player. Initially, the same deck of nn cards is given to both the dealer and the player. Each card in the deck suits with one of the four symbols (C,D,H,C, D, H, or SS), followed by the one of 13 kinds (2,3,4,5,6,7,8,9,10,A,J,K2, 3, 4, 5, 6, 7, 8, 9, 10, A, J, K or QQ). Therefore, there are 52 different types of cards and the same cards can exist in the deck. After the cards are given to the dealer and the player, the dealer first creates their own deck XX from the deck given to the dealer using any shuffling method and shows XX to the player. After that, the player creates the deck YY by the following steps: YY is initially empty.

Step 1. Create two decks P1P1 and P2P2 from the deck given to the player. The number of cards in P1P1 and P2P2 can be different.

Step 2. Interleave P1P1 and P2P2. That is, move a card at the bottom of P1P1 or P2P2 to the current top of YY, until there is no card on both P1P1 and P2P2. Note that the player does not need to move the cards in P1P1 and P2P2 alternately to YY. Also, since both the dealer and the player create their own deck from the same deck of nn cards, YY always consists of the same cards as XX.

We define a sequence of a deck as the sequence of the cards in the deck from bottom to top. Then the player’s score is defined as the length of the longest common subsequence between the sequences XX and YY. For example, suppose the deck of n=5n = 5 cards, (C2,CJ,D5,HA,S7)(C2, CJ, D5, HA, S7) is given to both the dealer and the player (here, we represent the deck as its sequence). Then the dealer creates the deck X=(CJ,D5,HA,C2,S7)X = (CJ, D5, HA, C2, S7) and shows XX to the player. After that, the player creates their deck by (i) creating two decks P1=(D5,HA)P1 = (D5, HA) and P2=(CJ,S7,C2)P2 = (CJ, S7, C2) from the given deck and (ii) create Y=(D5,CJ,S7,HA,C2)Y = (D5, CJ, S7, HA, C2) by interleaving P1P1 and P2P2. In this example, the player’s score is 3 since (CJ,HA,C2)(CJ, HA, C2) is the longest common subsequences between the sequences of XX and YY. Now, after finishing Step 1, the player wants to know the maximum possible score that the player can achieve after applying Step 2. For example, the maximum possible score from XX and YY in the previous example is 4 since it is possible to create YY from P1P1 and P2P2 as (CJ,D5,HA,S7,C2)(CJ, D5, HA, S7, C2).

Given n,X,P1n, X, P1 and P2P2, write a program to compute the maximum possible score that the player can achieve.

输入格式

Your program is to read from standard input. The input starts with a line containing three positive integers nn, pp and qq (3n5003 \leq n \leq 500, p+q=np + q = n), where nn is the number of cards in the initial deck, and pp and qq are the number of cards in P1P1 and P2P2, respectively. In the following three lines, the dealer’s deck XX consisting of nn cards, and the player’s two decks P1P1 and P2P2 consisting of pp and qq cards, respectively, are given. Each card in XX, P1P1, and P2P2 is represented as its suit (uppercase alphabet C,D,H,C, D, H, or SS) followed by its kind (2,3,4,5,6,7,8,9,102, 3, 4, 5, 6, 7, 8, 9, 10, uppercase alphabet A,J,KA, J, K or QQ). The cards in the same line are ordered from bottom to top of the corresponding deck.

输出格式

Your program is to write to standard output. Print exactly one line. The line should contain the maximum possible score that the player can achieve from XX, P1P1 and P2P2 after applying Step 2.

5 2 3
CJ D5 HA C2 S7
D5 HA
CJ S7 C2
4
6 3 3
C9 HK SQ SQ H2 CA
CA HK SQ
H2 C9 SQ
4
7 3 4
S9 C10 DJ S6 S7 SA DQ
DJ S6 S7
S9 C10 SA DQ
7