목차

Prefix와 Suffix

ps
링크acmicpc.net/…
출처BOJ
문제 번호13576
문제명Prefix와 Suffix
레벨플래티넘 2
분류

문자열

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python 3.11
제출기록48604KB / 240ms
최고기록240ms
해결날짜2022/12/17

풀이

코드

코드 1 - fail 함수 이용

"""Solution code for "BOJ 13576. Prefix와 Suffix".

- Problem link: https://www.acmicpc.net/problem/13576
- Solution link: http://www.teferi.net/ps/problems/boj/13576

Tags: [KMP]
"""

import collections
from teflib import string as tstring


def main():
    S = input()
    fail = tstring.failure_table(S)

    counter = collections.Counter(fail)
    for i in range(len(S) - 1, 0, -1):
        counter[fail[i - 1]] += counter[i]
        counter[i] += 1
    counter[len(S)] = 1

    l = len(S)
    border_lens = [len(S)]
    while l := fail[l - 1]:
        border_lens.append(l)

    print(len(border_lens))
    for x in reversed(border_lens):
        print(x, counter[x])


if __name__ == '__main__':
    main()

코드 2 - z 배열 이용

"""Solution code for "BOJ 13576. Prefix와 Suffix".

- Problem link: https://www.acmicpc.net/problem/13576
- Solution link: http://www.teferi.net/ps/problems/boj/13576

Tags: [Z algorithm]
"""

import collections
from teflib import string as tstring


def main():
    S = input()
    z = tstring.z_array(S)

    counter = collections.Counter(z)
    for i in reversed(range(len(S))):
        counter[i] += counter[i + 1]
    border_lens = [i for i, z_i in enumerate(reversed(z), start=1) if i == z_i]

    print(len(border_lens))
    for x in border_lens:
        print(x, counter[x])


if __name__ == '__main__':
    main()