题意:
题目没搞清楚,结果卡了好久
题目求a,b连续的数字相同(注意不是顺序相同)的子集的个数
思路:
第一个串作为模板, 记录每个位置的数字的值,然后第二个串记录每个值的位子,这么做的目的是为了计算在扫描过的模板串内的字母在第二个串内的长度(通过最大最小位置相减)
如果长度与模板串相同,那么一定是一样的,即所有在最小位置之前的都不在串内,所有大于最大位置的都不再串内
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 3005;
int n;
int mod[maxn], num[maxn];
int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i=1; i<=n; i++) {
scanf("%d", &mod[i]);
}
int x;
for(int i=1; i<=n; i++) {
scanf("%d", &x);
num[x] = i;
}
int ans = 0;
for(int i=1; i<=n; i++) {
int l = num[mod[i]], r=num[mod[i]];
int c=1;
for(int j=i+1; j<=n; j++) {
if(l>num[mod[j]]) l = num[mod[j]];
if(r<num[mod[j]]) r = num[mod[j]];
c++;
if(r-l+1 == c) ans++;
}
}
printf("%d\n", ans);
}
return 0;
}