kmp + 树状数组。树状数组动态更新排名。明白了方法以后,细节方面调了半天。。。想吐了。。。逗b的地方也不想改了。注意代码中注释的地方。
真的要想清楚再写,不然很浪费时间。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define N 110005
#define INF 1000000007
#define M 110005
int n, s, k;
int c[30];
int a[N];
int b[M];
int next[M];
int m0[N];
int m1[N];
int sub[M];
int ptr = 0;
int ans[N];
int lowbit(int x){
return x & (- x);
}
void add(int x, int v){
while(x <= s){
c[x] += v;
x += lowbit(x);
}
}
int sum(int x){
int sum = 0;
while(x > 0){
sum += c[x];
x -= lowbit(x);
}
return sum;
}
void gnext(){
int i = 1;
int j = 0;
next[0] = -1;
next[1] = 0;
while(i < k){
if(j == -1 || (m0[j] == sum(b[i]) && m1[j] == sum(b[i] - 1))){
add(b[i], 1);
i++;
j++;
if(m0[j] == sum(b[i]) && m1[j] == sum(b[i] - 1))next[i] = next[j];
else next[i] = j;
}
else {
for(int z = i - j; z < i - next[j]; z++){//注意起点与重点,可输出对add函数的调用验证
add(b[z], -1);
}
j = next[j];
}
}
}
void kmp(){
int i = 0;
int j = 0;
while(i < n){
if(j == -1 || ((m0[j] == sum(a[i]) && m1[j] == sum(a[i] - 1)))){
add(a[i], 1);
i++;
j++;
}
else {
for(int z = i - j; z < i - next[j]; z++)
add(a[z], -1);
j = next[j];
}
if(j == k){
for(int z = i - j; z < i - next[j]; z++)
add(a[z], -1);
ans[ptr++] = i - j + 1;
j = next[j] ;
}
}
}
int main(){
while(scanf("%d%d%d", &n, &k, &s) != EOF){
ptr = 0;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
memset(c, 0, sizeof(c));
for(int j = 0; j < k; j++){
scanf("%d", &b[j]);
m0[j] = sum(b[j]);
m1[j] = sum(b[j] - 1);
add(b[j], 1);
}
memset(c, 0, sizeof(c));
gnext();
memset(c, 0, sizeof(c));
kmp();
printf("%d\n", ptr);
for(int i = 0; i < ptr; i++)
printf("%d\n", ans[i]);
}
return 0;
}