접두사

last update: 2021.09.08.WED

1141 접두사, Silver 3

Memory 32932KB, Time 104ms


import sys
from collections import deque

n = int(input()) #length
words = set([sys.stdin.readline().strip() for _ in range(n)])
words = deque(sorted(words, key=lambda x:len(x)))

def checkword(words):
    count = 0
    result = 0
    for i in range(len(words)-1):
        prev = words[i]
        for j in range(i+1, len(words)):
            next = words[j]
            for i in range(len(prev)):
                if prev != next[:len(prev)]:
                    break
                elif prev == next[:len(prev)]:
                    count = 1

        result += count
        count = 0    

    return result

print(len(words)-checkword(words))


  • 런타임 에러가 났던 코드
import sys
from collections import deque

n = int(input()) #length
words = set([sys.stdin.readline().strip() for _ in range(n)])
words = deque(sorted(words, key=lambda x:len(x)))


def checkword(words):
    for i in range(len(words)-1):
        prev = words[i]
        for j in range(i+1, len(words)):
            next = words[j]
            for i in range(len(prev)):
                if prev != next[:len(prev)]:
                    words.rotate(-1)
                    break
                if prev == next[:len(prev)]:
                    words.poplet()
                    return checkword(words)
    return words

print(len(checkword(words)))