일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- Python
- 715. Range Module
- t1
- Convert Sorted List to Binary Search Tree
- Python Implementation
- attribute
- shiba
- iterator
- 파이썬
- DWG
- 프로그래머스
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 컴퓨터의 구조
- Python Code
- 밴픽
- Decorator
- 시바견
- 운영체제
- Generator
- Protocol
- Substring with Concatenation of All Words
- LeetCode
- kaggle
- concurrency
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- data science
- Regular Expression
- Class
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 |