标签:des style http io os ar for sp div
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> #include<algorithm> #include<cmath> #include<queue> #include<vector> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 1000005 int n,m; int a[N],b[N]; int next[N]; void getfail(int *b) { int i,j; next[0]=-1; j=-1; i=0; while(i<m) if(j==-1||b[i]==b[j]) { i++; j++; next[i]=j; } else j=next[j]; } int kmp(int *a,int *b) { int i,j; i=j=0; while(i<n) { if(j==-1||a[i]==b[j]) { i++; j++; } else j=next[j]; if(j==m) return i-j+1; } return -1; } int main() { int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<m;i++) scanf("%d",&b[i]); getfail(b); printf("%d\n",kmp(a,b)); } return 0; }
标签:des style http io os ar for sp div
原文地址:http://blog.csdn.net/u014737310/article/details/40350445