【Leetcode】【Easy】Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
?
解題:
本題為典型的KMP算法考察題,KMP算法描述為:
設(shè)主串S,匹配串P,i為S的索引下標(biāo),j為P的索引下標(biāo),初始i和j設(shè)置為0。
在i小于S的長(zhǎng)度和j小于P的長(zhǎng)度時(shí),開始循環(huán):
1、比較S[i]和P[j]是否相同;
2、如果相同則i++,j++,返回執(zhí)行第1步;
3、如果不同,則計(jì)算已匹配成功的P[0]~P[j-1]中,相同前綴和后綴的最大長(zhǎng)度,設(shè)為n;
4、令i不變,j為n(指向相同前后綴的后一個(gè)字符),返回執(zhí)行第1步;
循環(huán)結(jié)束時(shí),查看j值是否等于P的長(zhǎng)度,如果等于則匹配成功,否則主串中不包含匹配串。
計(jì)算最長(zhǎng)相同前后綴:
新建一個(gè)數(shù)組a,數(shù)組長(zhǎng)度為匹配串P長(zhǎng)度。數(shù)組的每一位a[i],表示由P[0]~P[i]表示的字符串,最長(zhǎng)相同前后綴的位數(shù);
a[0]初始化為0,令i為1,j為0,對(duì)于a[i](0<i<len)有兩種情況:
1、如果P[j] == P[i],那么a[i] = a[i - 1] + 1;
?接著j++,i++重新執(zhí)行第一步;
2、當(dāng)P[j] !=?P[i],如果j此時(shí)為0,表示由P[0]~P[i]組成的字符串沒有相同的前綴和后綴,所以a[i]=0,i++繼續(xù)進(jìn)行第一步;
3、當(dāng)P[j] !=?P[i],并且j不為0,表示可能包含相同前綴和后綴,則令j = a[j - 1],繼續(xù)執(zhí)行第一步;
直到計(jì)算出所有a[i]的值。
?
AC代碼見:
1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 int num = strlen(needle); 5 int *next = new int[num]; 6 getNext(needle, next); 7 8 int i = 0; 9 int j = 0; 10 while (haystack[i] != '\0' && needle[j] != '\0') { 11 if (haystack[i] == needle[j]) { 12 ++i; 13 ++j; 14 } else if (j == 0) { 15 ++i; 16 } else { 17 j = next[j - 1]; 18 } 19 } 20 21 free(next); 22 if (needle[j] != '\0') 23 return -1; 24 else 25 return i - j; 26 } 27 28 void getNext(char *needle, int *next) { 29 int i = 1; 30 int j = 0; 31 next[0] = 0; 32 while (needle[i] != '\0') { 33 if (needle[i] == needle[j]) { 34 next[i] = j + 1; 35 ++i; 36 ++j; 37 } else if (j == 0) { 38 next[i] = 0; 39 ++i; 40 } else { 41 j = next[j - 1]; 42 } 43 } 44 } 45 }; View Code?
代碼優(yōu)化:
實(shí)際編寫中,為了避免判定j是否為0,簡(jiǎn)化操作。可以設(shè)定next數(shù)組,取代a數(shù)組。next的含義是,當(dāng)kmp算法需要尋找子串下一個(gè)比較的位置時(shí),直接從next數(shù)組中取值;
其中next[0] = -1作為哨兵位,next[i] = a[i - 1],即將a數(shù)組整體后移一位保存在next數(shù)組中。
AC代碼如下:
1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 int num = strlen(needle); 5 int *next = new int[num]; 6 getNext(needle, next); 7 8 int i = 0; 9 int j = 0; 10 while (haystack[i] != '\0' && j < num) { 11 if (j == -1 || haystack[i] == needle[j]) { 12 ++i; 13 ++j; 14 } else { 15 j = next[j]; 16 } 17 } 18 19 free(next); 20 if (needle[j] != '\0') 21 return -1; 22 else 23 return i - j; 24 } 25 26 void getNext(char *needle, int *next) { 27 int i = 0; 28 int j = -1; 29 int strl = strlen(needle); 30 next[0] = -1; 31 while (i < strl - 1) { 32 if (j == -1 || needle[i] == needle[j]) { 33 next[++i] = ++j; 34 } else { 35 j = next[j]; 36 } 37 } 38 } 39 };?
轉(zhuǎn)載于:https://www.cnblogs.com/huxiao-tee/p/4227362.html
總結(jié)
以上是生活随笔為你收集整理的【Leetcode】【Easy】Implement strStr()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 张艺谋拍主旋律为啥没人喷?
- 下一篇: 和朋友下周来西安玩,有好玩的推荐吗?