Gene Shuffle
InterviewTime limit1sMemory limit128 MB
Two permutations of 1..N are given; split [1,N] into the shortest pieces where each piece holds the same set of genes in both. - use plain English
- Level
Medium5 of 10
- Topics
- Prefix sum, Hash map
- Solved
- No attempts yet
Problem
The genomes of two related species — for example, cabbage and turnip — may share many of the same genes. Because genome transpositions occur during evolution, the order in which those genes appear can differ from one genome to the other.
Your task is to compare two gene sequences and find the segments that are common to both genomes: contiguous stretches of positions that contain exactly the same set of genes in both genomes, even though the genes may be arranged in a different order.
Here a gene is not spelled out with the bases A, C, G, and T. Instead, each gene is encoded as a single integer. The two genomes being compared use the same integers: each genome is a permutation of the numbers , where is the length of the genome.
A common segment is an interval with such that positions through of the first genome contain exactly the same set of genes (integers) as positions through of the second genome, possibly in a different order.
You must report the minimal common segments: the pieces of the finest partition of the whole genome into consecutive common segments. Concretely, place a boundary right after position whenever the first genes of the first genome form the same set as the first genes of the second genome (positions and are always boundaries). Each interval between two consecutive boundaries is one minimal common segment.
Input
The first line contains a single integer: the number of test cases that follow. Each test case has the following format:
- One line with an integer with : the length of the genome.
- One line with a permutation of the integers , giving the first genome.
- One line with a permutation of the integers , giving the second genome.
On each line, the integers are separated by single spaces.
Output
For each test case, print a single line listing the minimal common segments common to the two genomes, ordered from left to right. Write each segment as its start and end position joined by a hyphen (i-j), and separate consecutive segments with a single space.