标签:sample ble printf exists ane i++ scribe namespace etc
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<cstdio> #include<cstring> #include<cstdlib> using namespace std; inline int read(){ int x=0,c=getchar(),f=1; for(;c<48||c>57;c=getchar()) if(!(c^45)) f=-1; for(;c>47&&c<58;c=getchar()) x=(x<<1)+(x<<3)+c-48; return x*f; } int sub_l,tar_l,sub[10001],tar[1000001],p[10001]; inline void pre(){ for(int i=2,j=0;i<=sub_l;i++){ while(j&&sub[j+1]^sub[i]) j=p[j]; if(!(sub[j+1]^sub[i])) j++; p[i]=j; } } inline void kmp(){ int ans=-1; for(int i=1,j=0;i<=tar_l;i++){ while(j&&sub[j+1]^tar[i]) j=p[j]; if(!(sub[j+1]^tar[i])) j++; if(!(j^sub_l)){ ans=i-sub_l+1; break; } } printf("%d\n",ans); } int main(){ int T=read(); while(T--){ tar_l=read(),sub_l=read(); memset(p,0,sizeof(p)); for(int i=1;i<=tar_l;i++) tar[i]=read(); for(int j=1;j<=sub_l;j++) sub[j]=read(); pre(); kmp(); } return 0; }
标签:sample ble printf exists ane i++ scribe namespace etc
原文地址:http://www.cnblogs.com/keshuqi/p/6194068.html