๐น ๋น์ทํ ๋จ์ด
1. ๋ฌธ์ ๋งํฌ
2. ์ฝ๋
Python3
34072kb
824ms
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
from collections import Counter
N = int(input())
words = []
max_prefix_len = 0 # ์ ๋์ฌ์ ์ต๋ ๊ธธ์ด
for _ in range(N):
word = input()
max_prefix_len = max(max_prefix_len, len(word))
words.append(word)
# M์ ๊ธธ์ด์ ์ ๋์ฌ ๋ง๋ค๊ณ ํ์ธ
def check_prefix(M):
prefix = []
for w in words:
prefix.append(w[:M])
dict_prefix = Counter(prefix)
result = []
for key in dict_prefix.keys():
if dict_prefix[key] > 1:
result.append(key)
return result
prefix = []
# ๊ฐ์ฅ ๊ธด ์ ๋์ฌ ์ฐพ๊ธฐ
while max_prefix_len:
prefix = check_prefix(max_prefix_len)
if prefix:
break
max_prefix_len -= 1
idx = 0
# ์ ๋์ฌ๋ฅผ ํฌํจํ๋ ๊ฐ์ฅ ์ฒซ ๋จ์ด ์ฐพ๊ธฐ(์ ๋์ฌ๋ ์ฐพ๊ธฐ)
for i in range(len(words)):
if words[i][:max_prefix_len] in prefix:
prefix = words[i][:max_prefix_len]
idx = i + 1
print(words[i])
break
# ํ์ ๋ ์ ๋์ฌ์ ํด๋นํ๋ ๋ ๋ฒ์งธ ๋จ์ด ์ฐพ๊ธฐ
for i in range(idx, len(words)):
if words[i][:max_prefix_len] == prefix:
print(words[i])
break
3. ํด์ค
๋จ์ด์ ๊ฐ์๊ฐ ์ต๋ 20000๊ฐ ์ด๋ฏ๋ก 2๊ฐ์ ๋จ์ด์ฉ ์ง์์ง์ด ๊ฐ ์ํ๋ฒณ์ ํ์ธํ์ฌ ์ ๋์ฌ๋ฅผ ์ฐพ๋ 3์ค ์ค์ฒฉ๋ฌธ์ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋๋ฏ๋ก ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํด๋ดค๋ค.
๋ชจ๋ ๋จ์ด๋ฅผ ๊ณตํต์ ์ธ ๊ธธ์ด๋ก ์๋ผ ์ค๋ณต๋๋ ๋จ์ด๊ฐ ์๋ค๋ฉด ๊ทธ๊ฒ์ด ๊ฒน์น๋ ์ ๋์ฌ๊ฐ ๋ ๊ฒ์ด๊ณ ํด๋น ์ ๋์ฌ์ ๊ธธ์ด๊ฐ ๊ฐ์ฅ ๊ธธ๋๋ก ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค. ์ด๋ฅผ ์ํด ๋จ์ด์์ ๊ฐ์ฅ ๊ธด ๋จ์ด๋ฅผ ์ ๋์ฌ์ ์ต๋ ๊ธธ์ด๋ก ๋๊ณ ๋ฐ๋ณตํ ๋๋ง๋ค ๊ธธ์ด๋ฅผ 1์ฉ ์ค์ฌ๊ฐ๋ฉฐ ๊ฐ์ฅ ๊ธด ์ ๋์ฌ์ ๊ธธ์ด๋ฅผ ์ฐพ๋๋ค.
check_prefix์์ ์ ๋ ฅ๋ ์ ๋์ฌ์ ๊ธธ์ด๋งํผ ๋จ์ด๋ฅผ ์๋ฅธ ํ ๋์ ๋๋ฆฌ๋ก ๋ง๋ค์ด ์ ๋์ฌ์ ๊ฐ์๊ฐ 1์ด ๋๊ฒ๋๋ ์ ๋์ฌ๊ฐ ๊ฐ์ฅ ๊ธด ์ ๋์ฌ๊ฐ ๋๋ค. ์ด๋ ์ด๋ฅผ ๋ง์กฑํ๋ ์ ๋์ฌ๊ฐ ์ฌ๋ฌ ๊ฐ ์ผ ์ ์์ผ๋ ๋ฐฐ์ด์ ๋ด์๋๋ค.
์ ๋์ฌ๋ฅผ ์ฐพ๊ณ ๋ ํ ์ ๋ ฅ๋ฐ์ ์์๋๋ก ๋จ์ด๋ฅผ ํ์ธํ๊ณ ์ ๋์ฌ ๋ฐฐ์ด์ ํฌํจ๋ ์ ๋์ฌ๋ฅผ ๊ฐ์ง ๋จ์ด๋ฅผ ์ฐพ๊ณ ์ถ๋ ฅํ๋ค. ์ด๋ ์ด๋ค ์ ๋์ฌ๋ฅผ ์ฌ์ฉํ ์ง ํ์ ๋๋ค.(์ ๋์ฌ ๋ฐฐ์ด ์์์ ์ ๋ ฅ ์์๊ฐ ๊ฐ์ฅ ๋จ์ด์ ํฌํจ๋ ์ ๋์ฌ)
๋ฐ๊ฒฌ๋ ์์น ๋ค์๋ถํฐ ์ ๋์ฌ๊ฐ ํฌํจ๋ ๊ฐ์ฅ ์ ์์ ๋จ์ด๋ฅผ ํ์ํ์ฌ ์ถ๋ ฅํ๋ค.