Under construction

Still being built. Things may change

Pawn Shop

Interview

Time limit2sMemory limit1024 MB

Summary
Split the rearranged sequence b into as many consecutive groups as possible so that each group holds the same multiset of values as the same positions in a.
Level

Medium5 of 10

Topics
Hash map, Greedy
Solved
No attempts yet

Problem

You run a tight ship at the pawn shop. You arrange certain items in the window to be displayed to the street. You sometimes display the same type of item multiple times. For simplicity, we think of the items on display as a sequence of values, where each value is the type of item being shown.

For example, your display could be this sequence:

1,2,6,2,7,9,8,51, 2, 6, 2, 7, 9, 8, 5

After coming back from your latest vacation, you find that your staff has completely rearranged the display by moving items around. Yikes! For example, the display above could be rearranged to:

2,6,1,2,9,7,5,82, 6, 1, 2, 9, 7, 5, 8

You fear this could cause confusion and scare off repeat customers. But you do not have time to move items back to their original positions.

As a compromise, you will put dividers up in the window to partition the displayed items into groups of consecutive items. Each group must be a rearrangement of the item types that were in those positions in your preferred arrangement.

More precisely, let a1,…,aNa_1, \ldots, a_N denote the first sequence and b1,…,bNb_1, \ldots, b_N denote the second sequence. You may place dividers around i,i+1,…,ji, i+1, \ldots, j if bi,bi+1,…,bjb_i, b_{i+1}, \ldots, b_j is a rearrangement of ai,ai+1,…,aja_i, a_{i+1}, \ldots, a_j. You do not need to put a divider at the beginning or end of the sequence. If ai=bia_i = b_i, then a group may be formed using just this single index ii.

With the sequences above, you could place dividers # at three positions as shown here:

2 6 1 # 2 # 7 9 # 5 8

It is not possible to divide the sequence into more than four groups that have this property.

Given the two sequences, determine how to partition the new sequence into the maximum possible number of groups.

Input

The first line of input contains a single integer NN (1≤N≤300 0001 \leq N \leq 300\,000), the length of the two sequences.

The next line contains NN integers a1,…,aNa_1, \ldots, a_N (1≤ai≤1091 \leq a_i \leq 10^9), the original sequence.

The next line contains NN integers b1,…,bNb_1, \ldots, b_N (1≤bi≤1091 \leq b_i \leq 10^9), the rearranged sequence. The values b1,…,bNb_1, \ldots, b_N are a rearrangement of the values a1,…,aNa_1, \ldots, a_N.

Output

Display the rearranged sequence with a valid partition using dividers (#) that yields the maximum number of groups.

If there are multiple possible solutions, you may display any of them.

Examples3

  1. Example 1

    Input
    8
    1 2 6 2 7 9 8 5
    2 6 1 2 9 7 5 8
    
    Expected output
    2 6 1 # 2 # 9 7 # 5 8
    
  2. Example 2

    Input
    4
    1 1 2 1
    2 1 1 1
    
    Expected output
    2 1 1 # 1
    
  3. Example 3

    Input
    3
    1 2 5
    2 5 1
    
    Expected output
    2 5 1