Genome Evolution
Time limit1sMemory limit128 MB
Count the shared gene blocks of length above one that appear as consecutive runs in both chromosomes.
Problem
Xi is a developmental biologist who studies developmental distances between chromosomes. In Xi's simplified view a chromosome is a permutation of genes numbered to . Xi is building a measure of evolutionary distance between two chromosomes. In Xi's theory of evolution, any set of genes that lies together in both chromosomes is a positive witness that the two chromosomes are similar.
A positive witness is a pair of sequences and of the same length, where is a consecutive subsequence of the first chromosome, is a consecutive subsequence of the second chromosome, and is a permutation of . Each chromosome contains every gene exactly once, so once is fixed there is at most one matching . Count the positive witnesses of the two given chromosomes whose length is greater than one.
Input
The input contains several test cases. Each test case starts with a line containing the number of genes (). The next two lines contain the two chromosomes, one per line, each as a list of positive integers that uses every number from to exactly once. A line containing a single ends the input and is not a test case.
Output
For each test case, print one line with the number of positive witnesses for the two chromosomes.