목차

Slot Machines

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()