Double Deck
Time limit2sMemory limit1024 MB
Find the maximum number of matched pairs between two decks of N*K cards, where each mismatch discards the top card of one deck.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, String
- Solved
- No attempts yet
Problem
You are playing a new card game. Each of the two decks has cards, labeled with integers from to . Each label appears exactly times in each deck.
The rules are simple. You shuffle both decks and place them face up in front of you, so you always see the top card of each deck. If the top cards match, you take both and get one point. If they do not match, you must discard one of the two cards. Your goal is to score as many points as possible.
You have just finished a round. Given the layout of both decks, find the maximum score you could have gotten.
Input
The first line contains two integers and (, ). The second and third lines each contain integers (), which describe the layout of the decks. is the top card, is the next card, and so on. No integer appears more than times in either line.
Output
Print one integer, the maximum possible score.