2 13 5 1 2 1 2 3 1 2 3 1 3 2 1 2 1 2 3 1 3 13 5 1 2 1 2 3 1 2 3 1 3 2 1 2 1 2 3 2 1
6 -1
【一道没有任何杂质的纯KMP】
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 1000010; const int M = 10010; int nxt[M]; int P[N], T[M]; int n,m; void getnext(){ int j, k; j = 0; k = -1; nxt[0] = -1; while(j<m){ if (k==-1 || T[j]==T[k]){ nxt[++j] = ++k; } else{ k = nxt[k]; } } } int kmp(){ getnext(); int j, k; j = 0; k = 0; while(k<m && j<n){ if (k==-1 || T[k]==P[j]){ ++k;++j; if(k==m) return j-m+1; } else{ k = nxt[k]; } } return -1; } int main() { int t; scanf("%d", &t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",&P[i]); for(int i=0;i<m;i++) scanf("%d",&T[i]); printf("%d\n", kmp()); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/taluoyaxin/article/details/47444863