ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 13576 |
문제명 | Prefix와 Suffix |
레벨 | 플래티넘 2 |
분류 |
문자열 |
시간복잡도 | O(n) |
인풋사이즈 | n<=100,000 |
사용한 언어 | Python 3.11 |
제출기록 | 48604KB / 240ms |
최고기록 | 240ms |
해결날짜 | 2022/12/17 |
"""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()
"""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()