Two collectors compare their trading-card decks. Deck A and deck B each hold n cards described by power ratings, and deck B is known to be deck A shuffled into a new order -- it contains exactly the same multiset of power ratings, just rearranged (possibly with repeated ratings). For every card in deck A, in order, report the position of a matching card in deck B, so that reading off deck B at the reported positions reconstructs deck A exactly. Whenever more than one unused position in B could match a given card, always choose the smallest available index.
n.n integers -- deck A's power ratings, in order.n integers -- deck B's power ratings, in order. It is guaranteed that B is a rearrangement of A (the same values with the same multiplicities).Print n space-separated integers P[0], P[1], ..., P[n-1] -- 0-indexed positions into B -- such that B[P[i]] = A[i] for every i, no position of B is used more than once, and whenever a value could be matched to more than one still-unused position, the smallest such index is chosen.
1 <= n <= 10000 <= A[i], B[i] <= 100000Example 1
Input
3 12 28 46 46 28 12
Expected
2 1 0
Explanation
Deck A is [12, 28, 46]. Scanning deck B = [46, 28, 12] for each card of A in turn: 12 appears in B at index 2, 28 at index 1, and 46 at index 0. No rating repeats, so the mapping is just those positions in order: 2 1 0.
Example 2
Input
3 2 3 2 3 2 2
Expected
1 0 2
Explanation
Deck A is [2, 3, 2]. For the first card (2), B has two candidates at indices 1 and 2, so the smallest unused index, 1, is chosen: P[0]=1. For the second card (3), the only match in B is index 0: P[1]=0. For the third card (2), index 1 is already used, leaving index 2: P[2]=2. The mapping is 1 0 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →