模板
哪个分类不能有个模板呢...虽然这么傻逼的模板...不过还是总得贴一贴的是吧...
/*========================================================================== # Last modified: 2016-03-01 18:23 # Filename: a.cpp # Description: ==========================================================================*/ #define me AcrossTheSky #include <cstdio> #include <cmath> #include <ctime> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define lowbit(x) (x)&(-x) #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) #define FORP(i,a,b) for(int i=(a);i<=(b);i++) #define FORM(i,a,b) for(int i=(a);i>=(b);i--) #define ls(a,b) (((a)+(b)) << 1) #define rs(a,b) (((a)+(b)) >> 1) #define getlc(a) ch[(a)][0] #define getrc(a) ch[(a)][1] #define maxn 2000000 #define maxm 100000 #define pi 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using namespace std; typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*==================split line==================*/ int n,m; int a[maxn],b[maxn],p[maxn]; void getfail(int *a){ p[1]=0; int j=0; FORP(i,2,m){ while (j>0 && a[i]!=a[j+1]) j=p[j]; if (a[i]==a[j+1]) j=j+1; p[i]=j; } } void kmp(int *s,int *a){ int j=0; FORP(i,1,n){ while(j>0 && s[i]!=a[j+1]) j=p[j]; if (s[i]==a[j+1]) j++; if (j==m) { printf("%d\n",i-m+1); return; } } printf("-1\n"); } int main(){ int cas; read(cas); while (cas--){ read(n); read(m); FORP(i,1,n) read(a[i]); FORP(i,1,m) read(b[i]); getfail(b); kmp(a,b); } }
元素从1开始,j表示当前匹配的位...然后似乎就没什么好说的了...
然而为什么我的kmp比他们慢那么多????...有的慢400ms(一共就1400ms)就忍了...那些写的700ms是什么鬼..HASH的姿势也不会QAQ...
Sometimes it s the very people who no one imagines anything of. who do the things that no one can imagine.