KMP算法—字符串匹配、查找子串。
1.优点:非常快
2.视频解析地址(本人认为这个up主讲的很好,肯定能看懂,一共分两期。一期讲原理,一期讲代码):
3.力扣上的第28题可以用此方法解决,当然也可以用编程语言的内置函数。但刷题难道不是为了学习算法么?以下是我根据这个up主的代码,写的java代码:
class Solution {
public int strStr ( String haystack, String needle) {
int res= 0 ;
char [ ] text= haystack. toCharArray ( ) ;
char [ ] pattern= needle. toCharArray ( ) ;
if ( needle. length ( ) == 0 ) {
return 0 ;
}
res= Kmp_search ( text, pattern) ;
return res;
}
public void next_table ( char [ ] pattern, int [ ] next, int n) {
next[ 0 ] = 0 ;
int len= 0 ;
int i= 1 ;
while ( i< n) {
if ( pattern[ i] == pattern[ len] ) {
len++ ;
next[ i] = len;
i++ ;
} else {
if ( len> 0 ) {
len= next[ len- 1 ] ;
} else {
next[ i] = len;
i++ ;
}
}
}
}
public void mov_next ( int [ ] next, int n) {
for ( int i= n- 1 ; i> 0 ; i-- ) {
next[ i] = next[ i- 1 ] ;
}
next[ 0 ] = - 1 ;
}
public int Kmp_search ( char [ ] text, char [ ] pattern) {
int n= pattern. length;
int m= text. length;
int [ ] next= new int [ n] ;
int res= - 1 ;
next_table ( pattern, next, n) ;
mov_next ( next, n) ;
int j= 0 , i= 0 ;
while ( i< m) {
if ( j== n- 1 && text[ i] == pattern[ j] ) {
res= i- j;
j= next[ j] ;
return res;
}
if ( text[ i] == pattern[ j] ) {
i++ ; j++ ;
} else {
j= next[ j] ;
if ( j== - 1 ) {
i++ ;
j++ ;
}
}
}
return res;
}
}