Two automated warehouses each print a packing manifest: an ordered list of item-name tokens, one per pallet slot. A separate approvals list records specific unordered pairs of item names that quality inspectors have certified as interchangeable substitutes for each other -- this certification is not transitive (if item A may substitute for item B, and item B may substitute for item C, that does NOT by itself certify that A may substitute for C). The two manifests are considered equivalent when they contain the same number of slots and, for every slot index, either both manifests name the exact same item at that slot, or the two item names at that slot form one of the certified substitute pairs (in either order). Determine whether the two given manifests are equivalent.
Line 1: an integer n1, the number of slots in the first manifest.
Line 2: n1 space-separated item names (the first manifest).
Line 3: an integer n2, the number of slots in the second manifest.
Line 4: n2 space-separated item names (the second manifest).
Line 5: an integer k, the number of certified substitute pairs.
Each of the next k lines contains two item names: one certified substitute pair.
Print YES if the two manifests are equivalent, otherwise print NO.
1 <= n1, n2 <= 1000 0 <= k <= 2000 Each item name has length between 1 and 20 and consists of lowercase English letters only. No certified pair contains the same item name twice, and no unordered pair of item names is listed more than once in the approvals list.
Example 1
Input
3 cratebox toolkit sensor 3 container toolkit detector 2 cratebox container sensor detector
Expected
YES
Explanation
Both manifests have 3 slots. Slot 1: "cratebox" vs "container" -- not identical, but (cratebox, container) is a certified pair, so they match. Slot 2: "toolkit" vs "toolkit" -- identical, so they match. Slot 3: "sensor" vs "detector" -- not identical, but (sensor, detector) is a certified pair, so they match. All 3 slots match, so the manifests are equivalent: YES.
Example 2
Input
3 cratebox toolkit sensor 3 container toolkit widget 2 cratebox container sensor detector
Expected
NO
Explanation
Slots 1 and 2 match exactly as in Example 1. Slot 3: "sensor" vs "widget" -- not identical, and no certified pair connects "sensor" and "widget" (only sensor/detector was certified). Since slot 3 fails to match, the manifests are not equivalent: NO.
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 →