Boyer-Moore 탐색 알고리즘
보이어무어 탐색법은 브루트포스 탐색 및 KMP 탐색보다 효율이 좋은 알고리즘이다.
int search(String text, String pattern){
int txtIndex;
int pattIndex;
int txtLen = text.length();
int patLen = pattern.length();
int[] skip = new int[Character.MAX_VALUE + 1];
for(txtIndex = 0; txtIndex <= Character.MAX_VALUE; txtIndex++){
skip[txtIndex] = patLen;
}
for(txtIndex = 0; txtIndex < patLen - 1 ; txtIndex++){
skip[pattern.charAt(txtIndex)] = patLen - txtIndex - 1;
}
while(txtIndex < txtLen){
pattIndex = patLen - 1;
while (text.charAt(txtIndex) == pattern.charAt(pattIndex)) {
if(pattIndex == 0){
return txtIndex;
}
pattIndex--;
txtIndex--;
}
// pt += (skip[txt.charAt(pt)] > patLen - pp) ? skip[txt.charAt(pt)] : patLen - pp;
txtIndex += Math.max(skip[text.charAt(txtIndex)], patLen - pattIndex);
}
return -1;
}
'데이터구조와 알고리즘 > 개념정리' 카테고리의 다른 글
[탐색 알고리즘] KMP법으로 문자열 탐색하기 (0) | 2021.09.25 |
---|---|
[탐색 알고리즘] 브루트포스법(brute force)으로 문자열 탐색하기 (0) | 2021.09.24 |
정렬 알고리즘5 - 퀵정렬(Quick Sort) (0) | 2021.09.01 |
정렬 알고리즘4 - 셸 정렬(Shell Sort) (0) | 2021.08.22 |
정렬 알고리즘3 - 삽입정렬(Insertion Sort) (0) | 2021.08.20 |