| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- DWG
- 운영체제
- kaggle
- 315. Count of Smaller Numbers After Self
- LeetCode
- Class
- 컴퓨터의 구조
- Python Code
- Regular Expression
- Generator
- shiba
- iterator
- 파이썬
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- 715. Range Module
- Decorator
- t1
- attribute
- 밴픽
- Python
- 시바견
- 43. Multiply Strings
- concurrency
- Python Implementation
- Protocol
- data science
Archives
- Today
- Total
Scribbling
KMP 문자열 검색 알고리즘 본문
파이썬 코드로 작성한 KMP 알고리즘이다.
이해하기 어렵다면 아래 사이트를 참고하자.
https://bowbowbow.tistory.com/6
def getPi(pattern):
m, j = len(pattern), 0
pi = [0] * m
for i in range(1, m):
while j > 0 and pattern[i] != pattern[j]:
j = pi[j-1]
if pattern[i] == pattern[j]:
j += 1
pi[i] = j
return pi
def kmp(string, pattern):
ret = []
pi = getPi(pattern)
n, m, j = len(string), len(pattern), 0
for i in range(n):
while j > 0 and string[i] != pattern[j]:
j = pi[j-1]
if string[i] == pattern[j]:
if j == m - 1:
ret.append(i-m+1)
j = pi[j]
else:
j += 1
return ret
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
| Segment Tree, Python Implementation (0) | 2022.01.06 |
|---|---|
| Priority Queue (0) | 2021.12.22 |
| 위상 정렬 (0) | 2021.08.14 |
| 신장 트리, 최소 신장 트리, Kruskal Algorithm (0) | 2021.08.13 |
| 무향/방향 그래프 내 Cycle 판별 (0) | 2021.08.12 |