Post

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

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

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


2. ์ฝ”๋“œ

Python3 32140KB 2944ms

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
[2179] ๋น„์Šทํ•œ ๋‹จ์–ด

๐Ÿ’› ๋ฌธ์ œ
N๊ฐœ์˜ ์˜๋‹จ์–ด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋น„์Šทํ•œ ๋‘ ๋‹จ์–ด๋ฅผ ๊ตฌํ•ด๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋‘ ๋‹จ์–ด์˜ ๋น„์Šทํ•œ ์ •๋„๋Š” ๋‘ ๋‹จ์–ด์˜ ์ ‘๋‘์‚ฌ์˜ ๊ธธ์ด๋กœ ์ธก์ •ํ•œ๋‹ค.
์ ‘๋‘์‚ฌ๋ž€ ๋‘ ๋‹จ์–ด์˜ ์•ž๋ถ€๋ถ„์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๋ถ€๋ถ„๋ฌธ์ž์—ด์„ ๋งํ•œ๋‹ค.
์ฆ‰, ๋‘ ๋‹จ์–ด์˜ ์•ž์—์„œ๋ถ€ํ„ฐ M๊ฐœ์˜ ๊ธ€์ž๋“ค์ด ๊ฐ™์œผ๋ฉด์„œ M์ด ์ตœ๋Œ€์ธ ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
"AHEHHEH", "AHAHEH"์˜ ์ ‘๋‘์‚ฌ๋Š” "AH"๊ฐ€ ๋˜๊ณ , "AB", "CD"์˜ ์ ‘๋‘์‚ฌ๋Š” ""(๊ธธ์ด๊ฐ€ 0)์ด ๋œ๋‹ค.

์ ‘๋‘์‚ฌ์˜ ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€์ธ ๊ฒฝ์šฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๋•Œ์—๋Š” ์ž…๋ ฅ๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ œ์ผ ์•ž์ชฝ์— ์žˆ๋Š” ๋‹จ์–ด๋ฅผ ๋‹ต์œผ๋กœ ํ•œ๋‹ค.
์ฆ‰, ๋‹ต์œผ๋กœ S๋ผ๋Š” ๋ฌธ์ž์—ด๊ณผ T๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ,
์šฐ์„  S๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ œ์ผ ์•ž์ชฝ์— ์žˆ๋Š” ๋‹จ์–ด์ธ ๊ฒฝ์šฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ ,
๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๋„ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๋•Œ์—๋Š” ๊ทธ ์ค‘์—์„œ T๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ œ์ผ ์•ž์ชฝ์— ์žˆ๋Š” ๋‹จ์–ด์ธ ๊ฒฝ์šฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— N(2 โ‰ค N โ‰ค 20,000)์ด ์ฃผ์–ด์ง„๋‹ค.
๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด 100์ž ์ดํ•˜์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์˜๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— S๋ฅผ, ๋‘˜์งธ ์ค„์— T๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
๋‹จ, ์ด ๋‘ ๋‹จ์–ด๋Š” ์„œ๋กœ ๋‹ฌ๋ผ์•ผ ํ•œ๋‹ค.
์ฆ‰, ๊ฐ€์žฅ ๋น„์Šทํ•œ ๋‘ ๋‹จ์–ด๋ฅผ ๊ตฌํ•  ๋•Œ ๊ฐ™์€ ๋‹จ์–ด๋Š” ์ œ์™ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

9
noon
is
lunch
for
most
noone
waits
until
two
"""

n = int(input())

words = []

for _ in range(n):
    # ['noon', 'is', 'lunch', 'for', 'most', 'noone', 'waits', 'until', 'two']
    words.append(input())

ans = 0
word1, word2 = "", ""

for i in range(n):
    if len(words[i]) < ans:
        continue

    # ๊ทธ๋‹ค์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋น„๊ต
    for j in range(i + 1, n):

        # ์ž‘์€ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋น„๊ตํ•ด์•ผ๋จ (์•ˆ๊ทธ๋Ÿฌ๋ฉด list index ์ดˆ๊ณผ)
        length = min(len(words[i]), len(words[j]))

        if length < ans or words[i] == words[j]:
            continue

        cnt = 0
        for k in range(length):
            # ์ž‘์€ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋น„๊ตํ•˜๊ธฐ
            if words[i][k] == words[j][k]:
                cnt += 1

            else:
                break

        if cnt > ans:
            # ans์— ์ ‘๋‘์‚ฌ ๊ธธ์ด ๋Œ€์ž… (ํด ๋•Œ๋งˆ๋‹ค ๊ฐฑ์‹ ํ•˜๊ธฐ)
            word1, word2, ans = words[i], words[j], cnt

print(word1)
print(word2)


3. ํ•ด์„ค

  • 3์ค‘ for ๋ฌธ์ด๋ผ์„œ ์‹œ๊ฐ„ ์ดˆ๊ฐ€ ๋‚˜๋Š”๊ฑธ๊นŒ ์ƒ๊ฐํ–ˆ์œผ๋‚˜, ์•„๋ฌด๋ฆฌ ๊ตฌ์ƒํ•ด๋ด๋„ 3์ค‘์œผ๋กœ ๋˜์•ผ ํ•  ๊ฒƒ ๊ฐ™์Œ
  • ์ ‘๋‘์‚ฌ ๊ธธ์ด๋ฅผ ํด ๋•Œ๋งˆ๋‹ค ๊ฐฑ์‹ ํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ์ŠคํŠธ๋“ค์„ ์ €์žฅํ•˜์ง€ ์•Š์•˜์Œ
  • ๋น„๊ตํ•  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์€ ๋ฌธ์ž์—ด์˜ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋ฌธ์ž๋“ค์„ ๋น„๊ตํ•จ
This post is licensed under CC BY 4.0 by the author.