ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 16229 |
문제명 | 반복 패턴 |
레벨 | 플래티넘 4 |
분류 |
문자열 |
시간복잡도 | O(n) |
인풋사이즈 | n<=100,000 |
사용한 언어 | Python 3.11 |
제출기록 | 40660KB / 172ms |
최고기록 | 120ms |
해결날짜 | 2022/12/16 |
"""Solution code for "BOJ 16229. 반복 패턴".
- Problem link: https://www.acmicpc.net/problem/16229
- Solution link: http://www.teferi.net/ps/problems/boj/16229
Tags: [KMP algorithm]
"""
from teflib import string as tstring
def main():
N, K = [int(x) for x in input().split()]
S = input()
if K >= N:
print(N)
return
fail = tstring.failure_table(S)
answer = 0
border_len = fail[-1]
while border_len:
if -N % (N - border_len) <= K:
answer = N - border_len
border_len = fail[border_len - 1]
print(answer)
if __name__ == '__main__':
main()
"""Solution code for "BOJ 16229. 반복 패턴".
- Problem link: https://www.acmicpc.net/problem/16229
- Solution link: http://www.teferi.net/ps/problems/boj/16229
Tags: [Z algorithm]
"""
from teflib import string as tstring
def main():
N, K = [int(x) for x in input().split()]
S = input()
if K >= N:
print(N)
return
z = tstring.z_array(S)
z[0] = 0
for i, z_i in enumerate(reversed(z), start=1):
if i == z_i and -N % (N - i) <= K:
print(N - i)
break
else:
print('0')
if __name__ == '__main__':
main()