UVALive5052 Genome Evolution(想法题)

两排列子序列匹配计数
本文介绍了一种算法,用于统计两个1到n的排列中具有相同连续子序列的数量。通过对第二个序列中各元素的位置进行标记,并遍历第一个序列来查找连续子序列,该方法实现了高效地匹配计数。

题意:

给你两个1~n的排列A和B,统计A,B有多少个子集是相等,序列要求连续的,长度要大于1。

分析:

记录一下B序列的各个整数的位置,然后在从A开始统计,因为他是集合关系的相等,所以要求长度相同,元素相同,接着枚举长度的同时,又因为我们要求的序列是连续的,所以我们找出位置的最大和最小的位置,然后他们的长度如果和我们枚举的长度相等的话,那么这个显然就是正确的了

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int N = 3000 + 9;
int a[N],b[N],p[N];
int main() {
    //freopen("f.txt","r",stdin);
    int n,l,r;
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;i++)scanf("%d",&a[i]);
        for(int i=0;i<n;i++)scanf("%d",&b[i]),p[b[i]]=i;
        int ans=0;
        for(int i=0;i<n;i++){
            int cnt=1;
            l=r=p[a[i]];
            for(int j=i+1;j<n;j++){
                if(p[a[j]]>r)r=p[a[j]];
                if(p[a[j]]<l)l=p[a[j]];
                cnt++;
                if(r-l+1==cnt)ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
Sample Input
4
3  2  1  4
1  2  4  3
5
3  2  1  5  4
3  2  1  5  4
0
Sample Output
3
10

*/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值