[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; }
星星之火,终将成燎原之势