#P11004. [蓝桥杯 2024 省 Python B] 魔法巡游
[蓝桥杯 2024 省 Python B] 魔法巡游
Background
The statement of this problem is highly ambiguous. Below is a comparison between the Luogu version and the Lanqiao Cup official website testdata:
- Luogu: resonance happens as long as the digits appear.
- Lanqiao Cup official website: resonance requires that the two numbers share a pair among in common.
Because the statement is ambiguous, the testdata of this problem is for reference only.
Problem Description
In the Lanqiao Kingdom, two wizards, Xiao Lan and Xiao Qiao, are responsible for maintaining the order of space and time. Each of them has rune stones. Every stone is carved with a numeric symbol between and . Xiao Lan's stones are denoted as , and Xiao Qiao's are denoted as .
Their task is to travel through nodes in space-time using these rune stones. Each travel follows this rule: after Xiao Lan uses rune stone to reach a new node, Xiao Qiao must choose a rune stone with a larger index (that is, some with ) to go to the next node. Similarly, after Xiao Qiao arrives, Xiao Lan must choose a rune stone with index to continue the tour.
To travel successfully, the numeric symbols on two consecutively used rune stones must resonate. This resonance happens only when the numeric symbols contain at least one special element: Spark (digit ), Ripple (digit ), or Windwhisper (digit ). For example, in the symbol sequence , every pair of consecutive runes contains at least one resonance element, so it is a successful tour sequence. But does not work, because their resonance elements do not include any of Spark, Ripple, or Windwhisper.
Xiao Lan always starts first, using his rune stones to begin the tour.
Your task is to compute the maximum length of a space-time tour sequence that these two wizards can perform. Such a sequence has the form , where the indices satisfy , and every adjacent pair of rune stones in the sequence contains at least one resonance element.
Input Format
The first line contains an integer , representing the number of rune stones each wizard has.
The second line contains integers , separated by spaces, representing the numeric symbols carved on Xiao Lan's rune stones.
The third line contains integers , separated by spaces, representing the numeric symbols carved on Xiao Qiao's rune stones.
Output Format
Output one line containing one integer, representing the maximum number of space-time tours Xiao Lan and Xiao Qiao can perform while following all rules.
5
126 393 581 42 44
204 990 240 46 52
4
Hint
For of the test cases, , .
For all test cases, , .
Sample Explanation
Here, digit serves as a resonance element connecting and , and and . Digits and serve as resonance elements connecting and .
Translated by ChatGPT 5