hdu1171kmp果题
Posted randy-lo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu1171kmp果题相关的知识,希望对你有一定的参考价值。
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1711/
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define dbg(args) cout<<#args<<":"<<args<<endl; 17 #define inf 0x3f3f3f3f 18 #define maxn 1000010 19 int n,m,t; 20 int a[maxn],b[maxn],nxt[maxn]; 21 void getnxt() 22 { 23 nxt[0]=-1; 24 int i=-1,j=0; 25 while(j<m) 26 { 27 if(i==-1||b[i]==b[j]) 28 { 29 i++,j++; 30 if(b[i]==b[j])nxt[j]=nxt[i]; 31 else nxt[j]=i; 32 } 33 else i=nxt[i]; 34 } 35 } 36 int kmp() 37 { 38 int i=0,j=0; 39 while(i<n) 40 { 41 if(j==-1||a[i]==b[j])i++,j++; 42 else j=nxt[j]; 43 if(j==m) 44 { 45 return i-m+1; 46 } 47 } 48 return -1; 49 } 50 int main() 51 { 52 //freopen("input.txt","r",stdin); 53 //freopen("output.txt","w",stdout); 54 std::ios::sync_with_stdio(false); 55 scan(t); 56 while(t--) 57 { 58 scan(n); 59 scan(m); 60 f(i,0,n-1)scan(a[i]); 61 f(i,0,m-1)scan(b[i]); 62 getnxt(); 63 pf("%d ",kmp()); 64 } 65 }
以上是关于hdu1171kmp果题的主要内容,如果未能解决你的问题,请参考以下文章
hdu 1171 Big Event in HDU(01背包)