A Gene Shuffle Problem

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

https://odzkskevi.qnssl.com/406b40240f0809f4429e3ebc38e55f64?v=1535506365

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=100005;
int a[N],b[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int i;
        for(i=1; i<=n; i++)
        {
            int x;
        scanf("%d",&x);
            a[x]=i;
        }
        for(i=1; i<=n; i++)
            scanf("%d",&b[i]);
        int l=1;
        int r=0;
        int sum=0;
        for(i=1; i<=n; i++)
        {
            sum=max(sum,a[b[i]]);
            if(sum==i)
            {
                if(r==0)printf("%d-%d",l,sum);
                else printf(" %d-%d",l,sum);
                l=sum+1;
                r++;
            }
        }
       printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值