标签:
题目链接:
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 687 Accepted Submission(s): 262
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; int n,m,p,a[maxn],b[maxn],c[maxn],nex[maxn]; inline void makenext() { int k=-1,j=0;nex[0]=-1; while(j<m) { if(k==-1||b[j]==b[k]) { j++;k++; nex[j]=k; } else k=nex[k]; } } inline int kmp(int l) { int pb=0,pc=0,ans=0; while(pb<m&&pc<l) { if(pb==-1||b[pb]==c[pc])pb++,pc++; else pb=nex[pb]; if(pb==m)ans++,pb=nex[pb]; } return ans; } int main() { int t,Case=0; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&p); for(int i=0;i<n;i++)scanf("%d",&a[i]); for(int i=0;i<m;i++)scanf("%d",&b[i]); makenext(); int ans=0; for(int i=0;i<p;i++) { int cnt=0; for(int j=i;j<n;j+=p)c[cnt++]=a[j]; ans+=kmp(cnt); } printf("Case #%d: %d\n",++Case,ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5939429.html