A Gene Shuffle
Problem
The genomes of two related species, like that of cabbage and turnip, may contain similar genes. Their order in the genome may be different due to genome transpositions during evolution.
Your task is to compare two gene sequences, and to determine the segments that are common to the genomes, i.e., the segments that contain the same genes in both genomes (although in a possibly different order).
Genes in this application are not given by sequences of bases A, C, G and T as is usually done, but are coded as a single integer. The two genomes that are compared consist of the same set of integers, each a permutation of the numbers 1,... ,N, where N is the length of the genome. A common segment i-j of the genomes is an interval 1 ≤ i ≤ j ≤ N such that elements starting at position i and ending at position j of the first genome make up the same set of genes (integers) as the elements at the same positions in the second genome, but possibly in a different order. We are looking for minimal common segments, i.e., segments cannot contain smaller segments.
Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
• One line with one integer N, satisfying 1 ≤ N ≤ 100, 000: the length of the genome.
• One line with a permutation of the N integers 1,... ,N, representing the first genome.
• One line with a permutation of the N integers 1,... ,N, representing the second genome. Integers on the same line are separated by single spaces.
Output
For every test case in the input, the output should contain a single line, listing the minimal segments i-j common to the two genomes, ordered from left to right. Segments are separated by single spaces.
Example Input 2 10
1 2 3 6 4 7 5 8 9 10
3 2 1 4 5 6 7 8 10 9
5
2 1 4 5 3
2 4 5 3 1
Output
1-3 4-7 8-8 9-10
1-1 2-5
题目要求为:给两段基因序列,内容为从1--n,且数字必须在1--n之间的数字,求每段相同的基因序列(序列不能含有其他序列),相同序列就是和一样,且上下出现的数字也一样。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
///所有的数字必须出现2次,且在1--n范围内
long long a[100010],b[100010],c[100010];
int main()
{
long long t,n,i,d,x,y;
scanf("%lld",&t);
while(t--)
{
memset(c,0,sizeof(c));///用来判断某一段中各数字出现次数
scanf("%lld",&n);
for(i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
for(i=1; i<=n; i++)
{
scanf("%lld",&b[i]);
}
d = 1;
x = 0;
y = 0;
i=1;
int ch=0;///标记某一段中数字在不断更新中剩余个数
while(i<=n)
{
x+=a[i];
y+=b[i];
c[a[i]]++;
if(c[a[i]]==1)ch++;
else if(c[a[i]]==2)ch--;
c[b[i]]++;
if(c[b[i]]==1)ch++;
else if(c[b[i]]==2)ch--;
if(ch==0&&x==y)///在该序列中的所有数字都出现了2次,并且和相等
{
if(d==1)///起始段
printf("%lld-%lld",d,i);
else
printf(" %lld-%lld",d,i);
d = i+1;
}
i++;
}
printf("\n");
}
return 0;
}