您的位置:首页 > 其它

UVALive - 5052 Genome Evolution 贪心

2015-03-01 15:40 330 查看
题目大意: 给出两个序列,统计一下有多少的二元组(a,b)满足以下条件:a是序列A的连续子序列,b是序列B的连续子序列,且a,b包含的数字完全相同

解题思路:记录序列B每个数字所在的位置,用MIN记录序列a在序列B中的最左端的位置,MAX记录序列a在序列B中最右端的位置,num记录这个子序列有多少个数字,如果满足MAX - MIN + 1 == num就表示满足

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 3010
int num1[maxn], num2[maxn];
int pos[maxn];
int n;
int main() {
	while(scanf("%d",&n) == 1 && n) {
		for(int i = 1; i <= n; i++)
			scanf("%d",&num1[i]);
		for(int i = 1; i <= n; i++) {
			scanf("%d",&num2[i]);
			pos[num2[i]] = i;
		}

		long long ans = 0;
		for(int i = 1; i <= n; i++) {
			int MAX = pos[num1[i]], MIN = pos[num1[i]];
			int num = 1;
			for(int j = i + 1; j <= n; j++) {
				if(pos[num1[j]] > MAX)
					MAX = pos[num1[j]];
				if(pos[num1[j]] < MIN)
					MIN = pos[num1[j]];
				num++;
				if(MAX - MIN + 1 == num)
					ans++;	
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: