标签:
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 729 Accepted Submission(s): 277
1 //2016.10.08 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 const int N = 1000005; 8 const int inf = 0x3f3f3f3f; 9 int a[N], b[N], nex[N],n, m, p; 10 11 void pre_kmp(int b[], int m)//得到next数组 12 { 13 int i, j; 14 j = nex[0] = -1; 15 i = 0; 16 while(i < m) 17 { 18 while(j != -1 && b[i]!= b[j])j = nex[j]; 19 nex[++i] = ++j; 20 } 21 } 22 23 int kmp(int a[], int n, int b[], int m) 24 { 25 int ans = 0; 26 pre_kmp(b, m); 27 for(int pos = 0; pos < p; pos++) 28 { 29 int i = pos, j = 0; 30 while(i < n) 31 { 32 while(j != -1 && a[i] != b[j])j = nex[j]; 33 i += p; j++; 34 if(j >= m){ans++; j = nex[j];} 35 } 36 } 37 return ans; 38 } 39 40 int main() 41 { 42 int T, kase = 0; 43 scanf("%d", &T); 44 while(T--) 45 { 46 scanf("%d%d%d", &n, &m, &p); 47 for(int i = 0; i < n; i++)scanf("%d", &a[i]); 48 for(int i = 0; i < m; i++)scanf("%d", &b[i]); 49 printf("Case #%d: %d\n", ++kase, kmp(a,n,b,m)); 50 } 51 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/Penn000/p/5940268.html