목차

이민희진

ps
링크acmicpc.net/…
출처BOJ
문제 번호28064
문제명이민희진
레벨실버 5
분류

문자열

시간복잡도O(n^2*m)
인풋사이즈n<=100, m<=20
사용한 언어Python 3.11
제출기록31388KB / 88ms
최고기록76ms
해결날짜2023/05/27

풀이

코드

코드 1 - 나이브

"""Solution code for "BOJ 28064. 이민희진".

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

import itertools


def main():
    N = int(input())
    names = [input() for _ in range(N)]

    answer = 0
    for n1, n2 in itertools.combinations(names, 2):
        if (any(n2.endswith(n1[:i]) for i in range(1, len(n1) + 1))) or (
            any(n1.endswith(n2[:i]) for i in range(1, len(n2) + 1))
        ):
            answer += 1

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 효율적

"""Solution code for "BOJ 28064. 이민희진".

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

import itertools
from teflib import string as tstring


def main():
    N = int(input())
    names = [input() for _ in range(N)]

    answer = 0
    for n1, n2 in itertools.combinations(names, 2):
        l = min(len(n1), len(n2))
        for s in [n1 + n2, n2 + n1]:
            z = tstring.z_array(s)
            if any(i == z_i <= l for i, z_i in enumerate(reversed(z), start=1)):
                answer += 1
                break

    print(answer)


if __name__ == '__main__':
    main()