1. 程式人生 > >[JZOJ 5437] [NOIP2017提高A組集訓10.31] Sequence 解題報告 (KMP)

[JZOJ 5437] [NOIP2017提高A組集訓10.31] Sequence 解題報告 (KMP)

題目連結:

http://172.16.0.132/senior/#main/show/5437

題目:

題解:

發現滿足上述性質並且僅當A序列的子序列的差分序列與B序列的差分序列相同

於是我們把A變成差分序列,把B變成差分序列,做一次KMP就好了

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;

const int N=1e6+15;
int n,m;
int a[N],b[N],nxt[N];
inline 
int read(){ char ch=getchar();int s=0,f=1; while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9') {s=(s<<3)+(s<<1)+ch-'0';ch=getchar();} return s*f; } int main(){ freopen("sequence.in","r",stdin); freopen("sequence.out
","w",stdout); n=read();m=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i<=m;i++) b[i]=read(); for (int i=1;i<n;i++) a[i]=a[i+1]-a[i]; for (int i=1;i<m;i++) b[i]=b[i+1]-b[i]; n--;m--; nxt[1]=0; for (int i=2,j=0;i<=m;i++){ while
(j&&b[j+1]!=b[i]) j=nxt[j]; if (b[j+1]==b[i]) ++j; nxt[i]=j; } int ans=0; for (int i=1,j=0;i<=n;i++){ while (j&&(j==m||b[j+1]!=a[i])) j=nxt[j]; if (b[j+1]==a[i]) ++j; if (j==m) ans++; } printf("%d\n",ans); return 0; }