PivotOJ

Simple Solitaire

시간 제한: 1000ms메모리 제한: 1024MB출처: ICPC ECNA 2022-2023BOJ 27618

문제

There are many different varieties of solitaire (card games for one), but most require an area where you can lay out cards. Here's one you can do by simply holding the deck in your hand.

The game uses a standard 5252 card deck where each card has one of 44 suits -- spades (S), hearts (H), diamonds (D) or clubs (C) -- and one of 1313 ranks -- Ace (A), 22, 33, 44, 55, 66, 77, 88, 99, 1010 (T), Jack (J), Queen (Q), King (K). To play the game you hold the deck face down in your hand and starting turning over cards from the bottom of the deck to the top, face up. Each time you turn over a card you check if it matches the rank or suit of the card three positions before it in the previously turned-over cards. If they match in rank, you remove those two cards and the two cards between them; if they match in suit you remove just those two cards. These actions might cause a cascade effect as now one or more of the remaining turned-over cards might match the ranks or suits of the cards three positions before them. (More detailed rules for processing cascades are described below.) Once all actions in a cascade have been performed (some of which may have forced the processing of even more cascades), you turn over the next card. If all the cards are removed in a single pass through the deck, you win the game!

The start of a sample game is shown below: suppose the first seven cards turned over are the TH, 44C, KS, AD, 22S and 88H. The table below show the next few actions taken in the game -- suits and ranks are underlined when they match. Note that turning over the 88S results in a cascade of two actions.

Card Dealt Hand Action
--- TH 44C KS AD 22S 88H none
55D TH 44C KS AD 22S 88H 55D remove AD, 55D
66D TH 44C KS 22S 88H 66D none
88S TH 44C KS 22S 88H 66D 88S remove 22S, 88S
--- TH 44C KS 88H 66D remove TH, 88H
99H 44C KS 66D 99H none
KC 44C KS 66D 99H KC remove KS, 66D, 99H, KC
22C 44C 22C none

If there is ever a choice of actions (which can happen during a cascade), you should pick an action which removes four adjacent cards over an action that removes two non-adjacent cards. If several possible actions might result in removing the same number of cards, use the one that removes the most recently turned-over card. For example, if the 66D had been 66C in the example above, then in the fifth row of the table the 44C and 66C would have been removed instead of the TH and 88H.

You have one solitary task: given the order in which cards are turned over, determine the cards remaining at the end of the game.

입력

Input consists of four lines, each containing 1313 cards. Cards appear in the order in which they are turned over in the game. Each card is represented by two consecutive characters, the first indicating the rank of the card (from A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K) and the second indicating the suit of the card (from S, H, D, C). A single space will separate adjacent cards.

출력

Output the number of cards remaining at the end of the game followed by the remaining cards, using the format described in the Input section.

예제

예제 1

입력
TC 2C 6C TS KC QS QC 3C KD 8D JH JS KH
5D JD 2S 8S AS 9S 3D 5H 9C AH 4D 4C KS
JC 4S 7S 6D 2H 7C 8C 7D AD 7H TH 2D QH
8H 9H 5C TD 3S 6H 3H QD 5S 9D 4H 6S AC
출력
2 3S 9D
코드를 제출하려면 로그인하세요.