Post

๐ŸฆŠ ๋น„์Šทํ•œ ๋‹จ์–ด

1. ๋ฌธ์ œ ๋งํฌ

2469๋ฒˆ: ๋น„์Šทํ•œ ๋‹จ์–ด


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
N = int(input())

words = []
length = 30000
for i in range(N):
    word = input()
    words.append(word)

S = -1
T = -1
max_count = -1
for i in range(N):
    left = words[i]
    for j in range(i + 1, N):
        right = words[j]
        count = 0
        length = min(len(left), len(right))
        if left == right or left[0] != right[0]:
            continue
        for k in range(length):
            if left[k] != right[k]:
                break
            count += 1
        # ์ตœ๋Œ€ ์ ‘๋‘์‚ฌ ๊ฐฑ์‹ 
        if max_count < count:
            max_count = count
            S = i
            T = j

# ์•ž์—์„œ ๋ถ€ํ„ฐ ์ตœ๋Œ€ ์ ‘๋‘์‚ฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์ฐพ์Œ
print(words[S])
print(words[T])


3. ํ•ด์„ค

pypy3 112490kb 3136ms

  • ํ•ด์„ค

    ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ตœ์ ํ™”ํ–ˆ๋‹ค๊ฐ€ ์ž๊พธ ํ‹€๋ ค์„œ O(N^2)๋กœ ์ˆ˜์ •โ€ฆ.

    ๋ฌธ์ž์—ด์„ ์™„์ „ ํƒ์ƒ‰ํ•˜์—ฌ ์ตœ๋Œ€ ์ ‘๋‘์‚ฌ ๊ธธ์ด๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅ

    ๊ตฌํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ตœ์ข…์œผ๋กœ ์ถœ๋ ฅ

This post is licensed under CC BY 4.0 by the author.