[20251205] BOJ / P5 / 가장 긴 팰린드롬 부분 문자열 / 권혁준 #1586
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/13275
🧭 풀이 시간
60분
👀 체감 난이도
✏️ 문제 설명
주어지는 문자열의 연속된 부분 문자열 중에서 팰린드롬인 것 중 가장 긴 것의 길이를 구해보자.
🔍 풀이 방법
manacher's algorithm에 대해 공부했다.A[i] = i번째 문자를 중심으로 하는 최대 팰린드롬의 반지름으로 정의한다.
현재까지 찾은 가장 긴 팰린드롬의 중심을 c, 반지름을 r이라고 가정한다.
문자열의 앞에서부터 보면서, 현재 인덱스 i에 대해 A[i] >= min(A[2c-i], r-i)이 성립한다.
따라서 일단 A[i] = min(A[2c-i], r-i)로 잡아놓고, 반복문으로 직접 양 옆으로 확장시켜 A[i]를 구한다.
모든 인덱스에 대한 A[i]값을 통해 정답을 구했다.
⏳ 회고
알고리즘의 동작 원리 자체는 금방 이해했다.
근데 이게 왜 O(N^2)이 아니라 O(N)인지 이해하는 게 어려웠다.
현재까지 찾은 팰린드롬의 오른쪽 끝 점은 c+r이고, 이 값보다 작은 지점에 대한 비교 연산을 수행하지 않기 때문에 O(N)이 되는 거였다.