日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NO6——KMP

發布時間:2025/3/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NO6——KMP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 int next[N]; 2 char str1[M],str2[N]; 3 //str1 長,str2 短 4 //len1,len2,對應str1,str2的長 5 6 void get_next(int len2) 7 { 8 int i = 0,j = -1; 9 next[0] = -1; 10 while(i<len2) 11 { 12 if(j == -1 || str2[i] == str2[j]) 13 { 14 i++; 15 j++; 16 if(str2[i] != str2[j]) 17 next[i] = j; 18 else 19 next[i] = next[j]; 20 } 21 else 22 j = next[j]; 23 } 24 //計算某字符串的周期,如aaaa是4,abcd是1 25 /* 26 int i = 0;j = -1; 27 next[0] = -1; 28 while(str2[i]) 29 { 30 if(j == -1 || str2[i] == str2[j]) 31 { 32 i++;j++; 33 next[i] = j; 34 } 35 else 36 j = next[j]; 37 } 38 len = strlen(str); 39 i = len-j; 40 if(len%i==0) 41 return len/i; 42 else 43 return 1; 44 */ 45 } 46 47 int kmp(int len1,int len2) 48 { 49 int i = 0,j = 0; 50 get_next(len2); 51 while(i<len1) 52 { 53 if(j == -1 || str1[i] == str2[j]) 54 { 55 i++; 56 j++ 57 } 58 else 59 j = next[j]; 60 /* 61 if(j == len2)//計算str2在str1中出現多少次 62 { 63 cnt++; 64 j= next[j]; 65 } 66 */ 67 } 68 //return j; //j為匹配的長度 69 if(j>len2) 70 return 1;//這里也可以返回i-len2來獲得匹配在主串中開始的位置 71 else 72 return 0; 73 } 74 75 //數字KMP 76 int a[1000005],b[10005]; 77 int next[10005],n,m; 78 79 void getnext() 80 { 81 int i = 0,j = -1; 82 next[0] = -1; 83 while(i<m) 84 { 85 if(j == -1 || b[i] == b[j]) 86 { 87 i++; 88 j++; 89 if(b[i] == b[j]) 90 next[i] = next[j]; 91 else 92 next[i] = j; 93 } 94 else 95 j = next[j]; 96 } 97 } 98 99 int kmp()//返回匹配位置 100 { 101 int i = 0,j = 0; 102 while(i<n) 103 { 104 if(a[i] == b[j]) 105 { 106 if(j == m-1) 107 return i-j+1; 108 i++; 109 j++; 110 } 111 else 112 { 113 j = next[j]; 114 if(j == -1) 115 { 116 i++; 117 j = 0; 118 } 119 } 120 } 121 return -1; 122 }

?

轉載于:https://www.cnblogs.com/xzxl/p/7305643.html

總結

以上是生活随笔為你收集整理的NO6——KMP的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。