| ps | |
|---|---|
| 링크 | acmicpc.net/… |
| 출처 | BOJ |
| 문제 번호 | 14959 |
| 문제명 | Slot Machines |
| 레벨 | 플래티넘 3 |
| 분류 |
문자열 |
| 시간복잡도 | O(n) |
| 인풋사이즈 | n<=1,000,000 |
| 사용한 언어 | Python 3.11 |
| 제출기록 | 171564KB / 372ms |
| 최고기록 | 372ms |
| 해결날짜 | 2022/12/16 |
"""Solution code for "BOJ 14959. Slot Machines".
- Problem link: https://www.acmicpc.net/problem/14959
- Solution link: http://www.teferi.net/ps/problems/boj/14959
Tags: [KMP]
"""
def failure_table(s):
"""Returns failure table of given string s."""
fail, j = [0], 0
for s_i in s[1:]:
while j > 0 and s_i != s[j]:
j = fail[j - 1]
if s_i == s[j]:
j += 1
fail.append(j)
return fail
def main():
n = int(input())
nums = input().split()
fail = failure_table(nums[::-1])
max_f = max(fail)
pos = fail.index(max_f)
k = n - pos - 1
p = pos + 1 - max_f
print(k, p)
if __name__ == '__main__':
main()