KMP算法 : 解决的是串匹配问题
next数组的求解
主要代码
求解next :
public static int [ ] getnext ( char [ ] str2) {
int n = str2. length;
int i = 0 ;
int [ ] next = new int [ str2. length] ;
next[ 0 ] = - 1 ;
next[ 1 ] = 0 ;
int cn = 0 ;
while ( i < next. length) {
if ( str2[ i - 1 ] == str2[ cn] ) {
next[ i++ ] = ++ cn;
} else if ( cn > 0 ) {
cn = next[ cn] ;
} else {
next[ i++ ] = 0 ;
}
}
return next;
}
求解匹配字符串的主函数 :
public static int getIndexOf ( String s, String m) {
if ( s == null || m == null || m. length ( ) < 1 || s. length ( ) < m. length ( ) ) {
return - 1 ;
}
char [ ] str1 = s. toCharArray ( ) ;
char [ ] str2 = m. toCharArray ( ) ;
int i1 = 0 ;
int i2 = 0 ;
int [ ] next = getnext ( str2) ;
while ( i1 < str1. length && i2 < str2. length) {
if ( str1[ i1] == str2[ i2] ) {
i1++ ;
i2++ ;
} else if ( next[ i2] == - 1 ) {
i1++ ;
} else {
i2 = next[ i2] ;
}
}
return i2 == str2. length ? i1 - i2 : - 1 ;
}