PivotOJ

Shuffle Game

시간 제한: 1500ms메모리 제한: 1024MB출처: ICPC Asia Seoul Regional 2022BOJ 26112

문제

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, or S), followed by the one of 13 kinds (2, 3, 4, 5, 6, 7, 8, 9, 10, A, J, K or Q). 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 P1P_1 and P2P_2 from the deck given to the player. The number of cards in P1P_1 and P2P_2 can be different.
  • Step 2. Interleave P1P_1 and P2P_2. That is, move a card at the bottom of P1P_1 or P2P_2 to the current top of YY, until there is no card on both P1P_1 and P2P_2. Note that the player does not need to move the cards in P1P_1 and P2P_2 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) is given to both the dealer and the player (here, we represent the deck as its sequence). Then the dealer creates the deck X=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=P_1 = (D5, HA) and P2=P_2 = (CJ, S7, C2) from the given deck and (ii) create Y=Y = (D5, CJ, S7, HA, C2) by interleaving P1P_1 and P2P_2. In this example, the player’s score is 33 since (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 to that the player can achieve after applying Step 2. For example, the maximum possible score from XX and YY in the previous example is 44 since it is possible to create YY from P1P_1 and P2P_2 as (CJ D5, HA, S7, C2).

Given nn, XX, P1P_1 and P2P_2, 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 (3 ≤ n ≤ 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 P1P_1 and P2P_2, respectively. In the following three lines, the dealer’s deck XX consisting of nn cards, and the player’s two decks P1P_1 and P2P_2 consisting of pp and qq cards, respectively, are given. Each card in XX, P1P_1, and P2P_2 is represented as its suit (uppercase alphabet C, D, H, or S) followed by its kind (2, 3, 4, 5, 6, 7, 8, 9, 10, uppercase alphabet A, J, K or Q). 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, P1P_1 and P2P_2 after applying Step 2.

예제

예제 1

입력
5 2 3
CJ D5 HA C2 S7
D5 HA
CJ S7 C2
출력
4

예제 2

입력
6 3 3
C9 HK SQ SQ H2 CA
CA HK SQ
H2 C9 SQ
출력
4

예제 3

입력
7 3 4
S9 C10 DJ S6 S7 SA DQ
DJ S6 S7
S9 C10 SA DQ
출력
7
코드를 제출하려면 로그인하세요.