[SWEA] 2007. 패턴 마디의 길이

야금야금 공부·2023년 5월 7일
0

SWEA

목록 보기
25/43
post-thumbnail

2007. 패턴 마디의 길이


문제 풀이

  • 첫 글자와 같은 문자의 index(=next)를 구해 그 전까지의 문자를 stack에 넣는다.
  • 만약 stack의 문자와 next부터의 같은 길이의 문자가 같다면 break
  • 만약 같지 않다면, next * 2부터 다시 같은 문자의 index를 탐색한다.
t = int(input())

for i in range(1, t + 1):

    arr = list(input())
    stack = []
    next = arr.index(arr[0], 1, 30)

    while stack[:next] != arr[next: 2 * next]:
        stack = []
        for a in range(next):
            stack.append(arr[a])

        if stack[:next] == arr[next: 2 * next]:
            break
        else:
            next = arr.index(arr[0], 2 * next, 30)

    print(f"#{i} {len(stack)}")

0개의 댓글