Created
November 5, 2012 10:41
-
-
Save caoxudong/4016571 to your computer and use it in GitHub Desktop.
KMP算法的实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KMP1 { | |
public static int kmp1(String pattern, String src){ | |
int srcLength = src.length() ; | |
int patternLength = pattern.length() ; | |
int index = -1 ; | |
int count = 0 ; | |
for(int i = 0 ; i < srcLength - 1 ;){ | |
count = 0 ; | |
for(int j = 0 ; (j < patternLength ) && (i < srcLength) ; j++){ | |
if(src.charAt(i) == pattern.charAt(j)){ | |
count++ ; | |
i++ ; | |
}else{ | |
break ; | |
} | |
} | |
if(count == patternLength){ | |
return i - count ; | |
} | |
if(count == 0){ | |
i++ ; | |
} | |
} | |
return index ; | |
} | |
public static void main(String[] args){ | |
String src = "1234567890abcdefghijk" ; | |
String pattern = "abcd" ; | |
long beginTime = System.nanoTime() ; | |
int index = kmp1(pattern, src) ; | |
System.out.println("My KMP : index = " + index + " and time = " + (System.nanoTime() - beginTime) + "ns"); | |
beginTime = System.nanoTime() ; | |
index = src.indexOf(pattern) ; | |
System.out.println("String.indexOf : index = " + index + " and time = " + (System.nanoTime() - beginTime) + "ns"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int kmp2(String pattern, String src){ | |
int srcLength = src.length() ; | |
int patternLength = pattern.length() ; | |
int index = -1 ; | |
int count = 0 ; | |
for(int i = 0 ; i < srcLength - 1 ;){ | |
count = 0 ; | |
for(int j = 0 ; (j < patternLength ) && (i < srcLength) && (src.charAt(i) == pattern.charAt(j)); j++,i++,count++) ; | |
if(count == patternLength){ | |
return i - count ; | |
} | |
if(count == 0){ | |
i++ ; | |
} | |
} | |
return index ; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
效果依然不好: | |
My KMP : index = 10 and time = 55805 ns | |
String.indexOf : index = 10 and time = 10277 ns | |
后续还需要再修改才行 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int kmp3(char[] patternArray , char[] srcArray){ | |
int index = -1 ; | |
int count = 0 ; | |
for(int i = 0 ; i < srcArray.length - 1 ;){ | |
count = 0 ; | |
for(int j = 0 ; (j < patternArray.length ) && (i < srcArray.length) && (srcArray[i] == patternArray[j]); j++,i++,count++) ; | |
if(count == patternArray.length){ | |
return i - count ; | |
} | |
if(count == 0){ | |
i++ ; | |
} | |
} | |
return index ; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
结果很悲剧,输出如下 | |
My KMP : index = 10 and time = 58012 ns | |
String.indexOf : index = 10 and time = 9895 ns | |
对代码进行修改,发现方法kmp1在内层循环中对条件判断的使用有些费操作,遂修改如下: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment