标签:view iss ref src php amp ota pen case
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1711
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42917 Accepted Submission(s): 17715
给出一串主串,一串子串;
求成功匹配到子串的最小下标;
KMP的应用,稍微变形,匹配到子串就跳出来输出。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 const int MAXN = 1e6+10; 9 const int MAXM = 1e4+10; 10 int W[MAXM], T[MAXN]; 11 int wlen, tlen; 12 int nxt[MAXM]; 13 14 void get_nxt() 15 { 16 int j, k; 17 j = 0; 18 k = -1; 19 nxt[0] = -1; 20 while(j < wlen){ 21 if(k == -1 || W[j] == W[k]){ 22 nxt[++j] = ++k; 23 } 24 else k = nxt[k]; 25 } 26 } 27 28 int KMP_index() 29 { 30 int i = 0, j = 0; 31 get_nxt(); 32 33 while( i < tlen && j < wlen){ 34 if(j == -1 || T[i] == W[j]){ 35 i++; 36 j++; 37 } 38 else j = nxt[j]; 39 } 40 if(j == wlen) return i-wlen+1; 41 else return -1; 42 } 43 44 int main() 45 { 46 int T_case, N, M; 47 scanf("%d", &T_case); 48 while(T_case--) 49 { 50 scanf("%d%d", &N, &M); 51 wlen = M, tlen = N; 52 for(int i = 0; i < N; i++) 53 scanf("%d", &T[i]); 54 for(int j = 0; j < M; j++) 55 scanf("%d", &W[j]); 56 57 printf("%d\n", KMP_index()); 58 } 59 return 0; 60 }
HDU 1711 Number Sequence 【KMP应用 求成功匹配子串的最小下标】
标签:view iss ref src php amp ota pen case
原文地址:https://www.cnblogs.com/ymzjj/p/10020540.html