题意:
给你两个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
*/
两排列子序列匹配计数

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

被折叠的 条评论
为什么被折叠?



