generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: ๐น 4์ 7 | ||
author: chaeshee0908 | ||
date: 2023-10-29 20:00:00 +09:00 | ||
categories: [์๊ณ ๋ฆฌ์ฆ, 4์ 7] | ||
tags: [์๊ณ ๋ฆฌ์ฆ, ๋ฐฑ์ค, ๊ณจ๋5, ๊ตฌํ, 5์ฃผ์ฐจ, ์ฑ์นํฌ] | ||
render_with_liquid: false | ||
--- | ||
|
||
## 1. ๋ฌธ์ ๋งํฌ | ||
|
||
[2877๋ฒ: 4์ 7](https://www.acmicpc.net/problem/2877) | ||
|
||
<br> | ||
|
||
## 2. ์ฝ๋ | ||
|
||
`Python3` `31120kb` `40ms` | ||
|
||
```python | ||
count = [2**i for i in range(31)] | ||
K = int(input()) | ||
|
||
num = 0 | ||
for i in range(1, 31): | ||
num += count[i] | ||
if K <= num: | ||
num_len = i # K๋ฒ์งธ ์ซ์์ ๊ธธ์ด | ||
break | ||
|
||
k_location = K - (num - count[num_len]) | ||
result = '' | ||
|
||
div_num = 2 | ||
while num_len: | ||
n = k_location % div_num | ||
if n == 0 or n > (div_num // 2): | ||
result = '7' + result | ||
else: | ||
result = '4' + result | ||
num_len -= 1 | ||
div_num *= 2 | ||
|
||
print(int(result)) | ||
``` | ||
|
||
<br> | ||
|
||
## 3. ํด์ค | ||
|
||
๋จผ์ K๋ฒ์งธ ์ซ์์ ๊ธธ์ด๋ฅผ ํ์ ํด์ค๋๋ค. ์ฌ๊ธฐ์๋ `num_len` | ||
|
||
๊ฐ์ฅ ์์ ์๋ฆฌ์ ๋ถํฐ ๊ฐ์ ๊ตฌํด์ค๋๋ค. ๊ฐ ์๋ฆฌ์๋ 4์ 7๋ง ๋ค์ด๊ฐ ์ ์์ผ๋ฏ๋ก ๊ฒฝ์ฐ์ ์๋ 2์ `num_len` ์ ๊ณฑ๋ฐฐ ์ ๋๋ค. | ||
|
||
ํ์ธํ๋ ์๋ฆฌ๊ฐ ํ ์นธ ์์ผ๋ก ๊ฐ ๋๋ง๋ค `div_num`(๋๋์ด์ฃผ๋ ์)์ 2๋ฅผ ๊ณฑํด์ค๋๋ค. ์ด๋ div_num์ผ๋ก ๋๋ ๋๋จธ์ง๊ฐ `div_num`์ 2๋ถ์ 1๋ฐฐ๋ณด๋ค ์๋ค๋ฉด โ4โ, ํฌ๋ค๋ฉด(ํน์ 0์ผ๋ก ๋๋์ด ๋จ์ด์ง๋ค๋ฉด) โ7โ์ ๊ฐ์ ์ฐ๊ฒฐํด์ค๋๋ค. | ||
|
||
K๋ฒ์งธ ์ซ์์ ๊ธธ์ด๋งํผ ๋ฐ๋ณตํด์ฃผ๊ณ ๋ฌธ์์ด๋ก ๋ค๋ค๋ ๊ฐ์ intํ์ผ๋ก ํ๋ณํ ํด์ค๋๋ค. |
106 changes: 106 additions & 0 deletions
106
_posts/algorithm/๊ฑฐ๋ฆฌ๋๊ธฐ-ํ์ธํ๊ธฐ/2023-12-03-์ฑ์นํฌ-๊ฑฐ๋ฆฌ๋๊ธฐ-ํ์ธํ๊ธฐ.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: ๐น ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ | ||
author: chaeshee0908 | ||
date: 2023-12-03 20:00:00 +09:00 | ||
categories: [์๊ณ ๋ฆฌ์ฆ, ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ] | ||
tags: [์๊ณ ๋ฆฌ์ฆ, ํ๋ก๊ทธ๋๋จธ์ค, level2, ๊ตฌํ, 7์ฃผ์ฐจ, ์ฑ์นํฌ] | ||
render_with_liquid: false | ||
--- | ||
|
||
## 1. ๋ฌธ์ ๋งํฌ | ||
|
||
[๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ](https://school.programmers.co.kr/learn/courses/30/lessons/81302) | ||
|
||
<br> | ||
|
||
## 2. ์ฝ๋ | ||
|
||
```python | ||
def solution(places): | ||
answer = [] | ||
for place in places: | ||
candidate = find_candidate(place) | ||
answer.append(check_observe_distance(place, candidate)) | ||
return answer | ||
|
||
def find_candidate(place): | ||
candidate = [] | ||
for i in range(5): | ||
for j in range(5): | ||
if place[i][j] == 'P': | ||
candidate.append((i, j)) | ||
return candidate | ||
|
||
def check_observe_distance(place, candidate): | ||
for i in range(len(candidate)-1): | ||
now_x, now_y = candidate[i] | ||
for j in range(i+1, len(candidate)): | ||
if i == j: | ||
continue | ||
next_x, next_y = candidate[j] | ||
manhattan = abs(now_x - next_x) + abs(now_y - next_y) | ||
diff_x, diff_y = abs(now_x - next_x), abs(now_y - next_y) | ||
# ๋งจํํผ ๊ฑฐ๋ฆฌ๊ฐ 1์ดํ์ธ ๊ฒฝ์ฐ | ||
if manhattan <= 1: | ||
return 0 | ||
# ๋งจํํผ ๊ฑฐ๋ฆฌ๊ฐ 2์ธ ๊ฒฝ์ฐ | ||
if manhattan == 2: | ||
# ๊ฐ๋ก ๊ธธ์ด 2์ธ ๊ฒฝ์ฐ ์ค ํํฐ์ ์๋ ๊ฒฝ์ฐ | ||
if diff_x == 2 and place[min(now_x, next_x)+1][now_y] != 'X': | ||
return 0 | ||
# ์ธ๋ก ๊ธธ์ด 2์ธ ๊ฒฝ์ฐ ์ค ํํฐ์ ์๋ ๊ฒฝ์ฐ | ||
elif diff_y == 2 and place[now_x][min(now_y, next_y)+1] != 'X': | ||
return 0 | ||
# ๋๊ฐ์ ์ธ ๊ฒฝ์ฐ ์ค ํํฐ์ ์๋ ๊ฒฝ์ฐ | ||
elif place[now_x][next_y] == 'O' or place[next_x][now_y] == 'O': | ||
return 0 | ||
return 1 | ||
``` | ||
- ์คํ ๊ฒฐ๊ณผ | ||
|
||
| ํ ์คํธ 1 ใ | ํต๊ณผ (0.16ms, 10.4MB) | | ||
| ํ ์คํธ 2 ใ | ํต๊ณผ (0.05ms, 10.3MB) | | ||
| ํ ์คํธ 3 ใ | ํต๊ณผ (0.04ms, 10.3MB) | | ||
| ํ ์คํธ 4 ใ | ํต๊ณผ (0.06ms, 10.2MB) | | ||
| ํ ์คํธ 5 ใ | ํต๊ณผ (0.04ms, 10.4MB) | | ||
| ํ ์คํธ 6 ใ | ํต๊ณผ (0.03ms, 10.2MB) | | ||
| ํ ์คํธ 7 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 8 ใ | ํต๊ณผ (0.08ms, 10.4MB) | | ||
| ํ ์คํธ 9 ใ | ํต๊ณผ (0.04ms, 10.3MB) | | ||
| ํ ์คํธ 10 ใ | ํต๊ณผ (0.03ms, 10.2MB) | | ||
| ํ ์คํธ 11 ใ | ํต๊ณผ (0.04ms, 10.2MB) | | ||
| ํ ์คํธ 12 ใ | ํต๊ณผ (0.03ms, 10.4MB) | | ||
| ํ ์คํธ 13 ใ | ํต๊ณผ (0.04ms, 10.2MB) | | ||
| ํ ์คํธ 14 ใ | ํต๊ณผ (0.03ms, 10.4MB) | | ||
| ํ ์คํธ 15 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 16 ใ | ํต๊ณผ (0.06ms, 10.2MB) | | ||
| ํ ์คํธ 17 ใ | ํต๊ณผ (0.05ms, 10.2MB) | | ||
| ํ ์คํธ 18 ใ | ํต๊ณผ (0.06ms, 10.3MB) | | ||
| ํ ์คํธ 19 ใ | ํต๊ณผ (0.07ms, 10.2MB) | | ||
| ํ ์คํธ 20 ใ | ํต๊ณผ (0.08ms, 10.1MB) | | ||
| ํ ์คํธ 21 ใ | ํต๊ณผ (0.07ms, 10.2MB) | | ||
| ํ ์คํธ 22 ใ | ํต๊ณผ (0.06ms, 10.4MB) | | ||
| ํ ์คํธ 23 ใ | ํต๊ณผ (0.02ms, 10.3MB) | | ||
| ํ ์คํธ 24 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 25 ใ | ํต๊ณผ (0.03ms, 10.4MB) | | ||
| ํ ์คํธ 26 ใ | ํต๊ณผ (0.02ms, 10.1MB) | | ||
| ํ ์คํธ 27 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 28 ใ | ํต๊ณผ (0.03ms, 10.2MB) | | ||
| ํ ์คํธ 29 ใ | ํต๊ณผ (0.05ms, 10.4MB) | | ||
| ํ ์คํธ 30 ใ | ํต๊ณผ (0.03ms, 10.4MB) | | ||
| ํ ์คํธ 31 ใ | ํต๊ณผ (0.05ms, 10.4MB) | | ||
|
||
<br> | ||
|
||
## 3. ํด์ค | ||
|
||
๊ฐ์์ค ๋ณ ์์์๋ค์ ์์น ์ ๋ณด๋ฅผ ๋ด์ candidate๋ฅผ ์์ฑํ ํ, ๊ฐ์์ค ๋ณ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ฅผ ํ์ ํ๋ค. | ||
|
||
๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ๋ค. | ||
|
||
1. ๋งจํํผ ๊ฑฐ๋ฆฌ = 1 | ||
2. ๋งจํํผ ๊ฑฐ๋ฆฌ = 2 | ||
1. ๊ฐ๋ก ๊ฑฐ๋ฆฌ ํน์ ์ธ๋ก ๊ฑฐ๋ฆฌ๊ฐ 2์ฐจ์ด ์ผ๋ ๊ฐ์ด๋ฐ ์์น์ ํํฐ์ ์ด ์๋ ๊ฒฝ์ฐ | ||
2. ๋๊ฐ์ ์ ์์์๋ค์ด ์์นํ ๋ ๋ฐ๋ ๋ฐฉํฅ ๋๊ฐ์ ์ค ํํฐ์ ์ด ํ๋๋ผ๋ ์๋ ๊ฒฝ์ฐ | ||
|
||
candidate ์์น๋ฅผ ๋น๊ตํ์ฌ ์์ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ผ ๋๋ 0์ ๋ฆฌํด, ๊ทธ ์ด์ธ๋ 1์ ๋ฆฌํดํ๋ check_observe_distance ํจ์๋ฅผ ์์ฑํ ํ answer์ ๊ฐ์ ๋ฃ์ด์ค๋ค. |
110 changes: 110 additions & 0 deletions
110
_posts/algorithm/๋ฏธ๋ก-ํ์ถ-๋ช
๋ น์ด/2023-12-03-์ฑ์นํฌ-๋ฏธ๋ก-ํ์ถ-๋ช
๋ น์ด.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: ๐น ๋ฏธ๋ก ํ์ถ ๋ช ๋ ์ด | ||
author: chaeshee0908 | ||
date: 2023-12-03 20:00:00 +09:00 | ||
categories: [์๊ณ ๋ฆฌ์ฆ, ๋ฏธ๋ก ํ์ถ ๋ช ๋ ์ด] | ||
tags: [์๊ณ ๋ฆฌ์ฆ, ํ๋ก๊ทธ๋๋จธ์ค, level3, ๊ตฌํ, 7์ฃผ์ฐจ, ์ฑ์นํฌ] | ||
render_with_liquid: false | ||
--- | ||
|
||
## 1. ๋ฌธ์ ๋งํฌ | ||
|
||
[๋ฏธ๋ก ํ์ถ ๋ช ๋ ์ด](https://school.programmers.co.kr/learn/courses/30/lessons/150365) | ||
|
||
<br> | ||
|
||
## 2. ์ฝ๋ | ||
|
||
```python | ||
dx = [1, 0, 0, -1] | ||
dy = [0, -1, 1, 0] | ||
move = ['d', 'l', 'r', 'u'] | ||
|
||
from collections import deque | ||
import sys | ||
|
||
def solution(n, m, x, y, r, c, k): | ||
sys.setrecursionlimit(5000) | ||
q = deque([]) | ||
q.append((x, y, 0, '')) | ||
# ์ต๋จ ๊ฑฐ๋ฆฌ ๊ธธ์ด | ||
min_dist = abs(x-r) + abs(y-c) | ||
|
||
# ์ต๋จ ๊ฑฐ๋ฆฌ๋ณด๋ค ์ด๋ ๊ฐ๋ฅ ๊ฑฐ๋ฆฌ๊ฐ ์๊ฑฐ๋ ๋๋ฌ์ด ๋ถ๊ฐ๋ฅํ ์์น์ผ ๋ | ||
if min_dist > k or (k - min_dist) % 2 == 1: | ||
return 'impossible' | ||
|
||
def find_path(a, b, count, path): | ||
# k๋ฒ์ผ๋ก ๋๋ฌํ ์ ์๋ ๊ฒฝ์ฐ | ||
if a == r and b == c and (k-count) % 2 == 1: | ||
return 'impossible' | ||
# k๋ฒ์งธ ๋๋ฌ | ||
if a == r and b == c and count == k: | ||
return path | ||
for i in range(4): | ||
na = a + dx[i] | ||
nb = b + dy[i] | ||
if 0 < na <= n and 0 < nb <= m: | ||
# ์ด๋ ์์น๊ฐ ์ต์ข ์์น์ ๋๋ฌํ ์ ์์๋งํผ ๋ฉ์ด์ง ๋ | ||
if abs(na-r) + abs(nb-c) > k - (count+1): | ||
continue | ||
result = find_path(na, nb, count+1, path + move[i]) | ||
return result | ||
|
||
return find_path(x, y, 0, '') | ||
``` | ||
|
||
- ์ ํ์ฑ | ||
|
||
| ํ ์คํธ 1 ใ | ํต๊ณผ (0.21ms, 10.4MB) | | ||
| ํ ์คํธ 2 ใ | ํต๊ณผ (0.15ms, 10.4MB) | | ||
| ํ ์คํธ 3 ใ | ํต๊ณผ (0.01ms, 10.4MB) | | ||
| ํ ์คํธ 4 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 5 ใ | ํต๊ณผ (0.04ms, 10.4MB) | | ||
| ํ ์คํธ 6 ใ | ํต๊ณผ (0.02ms, 10.4MB) | | ||
| ํ ์คํธ 7 ใ | ํต๊ณผ (0.02ms, 10.3MB) | | ||
| ํ ์คํธ 8 ใ | ํต๊ณผ (0.03ms, 10.3MB) | | ||
| ํ ์คํธ 9 ใ | ํต๊ณผ (10.15ms, 16MB) | | ||
| ํ ์คํธ 10 ใ | ํต๊ณผ (7.63ms, 16.1MB) | | ||
| ํ ์คํธ 11 ใ | ํต๊ณผ (9.03ms, 16.1MB) | | ||
| ํ ์คํธ 12 ใ | ํต๊ณผ (6.72ms, 16MB) | | ||
| ํ ์คํธ 13 ใ | ํต๊ณผ (7.26ms, 16.1MB) | | ||
| ํ ์คํธ 14 ใ | ํต๊ณผ (8.84ms, 16MB) | | ||
| ํ ์คํธ 15 ใ | ํต๊ณผ (6.66ms, 16.2MB) | | ||
| ํ ์คํธ 16 ใ | ํต๊ณผ (6.79ms, 15.7MB) | | ||
| ํ ์คํธ 17 ใ | ํต๊ณผ (6.54ms, 15.9MB) | | ||
| ํ ์คํธ 18 ใ | ํต๊ณผ (8.29ms, 15.9MB) | | ||
| ํ ์คํธ 19 ใ | ํต๊ณผ (6.31ms, 16.2MB) | | ||
| ํ ์คํธ 20 ใ | ํต๊ณผ (9.90ms, 15.8MB) | | ||
| ํ ์คํธ 21 ใ | ํต๊ณผ (8.67ms, 15.8MB) | | ||
| ํ ์คํธ 22 ใ | ํต๊ณผ (8.65ms, 15.8MB) | | ||
| ํ ์คํธ 23 ใ | ํต๊ณผ (13.91ms, 16.1MB) | | ||
| ํ ์คํธ 24 ใ | ํต๊ณผ (9.83ms, 16.1MB) | | ||
| ํ ์คํธ 25 ใ | ํต๊ณผ (6.76ms, 15.8MB) | | ||
| ํ ์คํธ 26 ใ | ํต๊ณผ (9.31ms, 15.8MB) | | ||
| ํ ์คํธ 27 ใ | ํต๊ณผ (9.86ms, 16MB) | | ||
| ํ ์คํธ 28 ใ | ํต๊ณผ (9.50ms, 15.8MB) | | ||
| ํ ์คํธ 29 ใ | ํต๊ณผ (9.52ms, 16MB) | | ||
| ํ ์คํธ 30 ใ | ํต๊ณผ (9.42ms, 16MB) | | ||
| ํ ์คํธ 31 ใ | ํต๊ณผ (0.01ms, 10.3MB) | | ||
|
||
<br> | ||
|
||
## 3. ํด์ค | ||
|
||
์ด ๋ฌธ์ ๋ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑธ๋ฌ๋ด์ฃผ์ด ์๊ฐ์ ์ค์ด๋ ๊ฒ์ด ์ค์ํ๋ค. | ||
|
||
๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ๋ค. | ||
|
||
1. (x, y) โ (r, c)์ ์ต๋จ ๊ฒฝ๋ก๋ณด๋ค k(์ด๋ ๊ฐ๋ฅ ๊ฑฐ๋ฆฌ)๊ฐ ์์ ๋ | ||
2. k(์ด๋ ๊ฐ๋ฅ ๊ฑฐ๋ฆฌ)์์ (x, y) โ (r, c)์ ์ต๋จ ๊ฒฝ๋ก ๊ธธ์ด๋ฅผ ๋บ ๊ธธ์ด๊ฐ 2์ ๋ฐฐ์๊ฐ ์๋ ๋ | ||
|
||
โ ์ต๋จ ๊ฑฐ๋ฆฌ ์ด๋ ํ ์ข,์ฐ ํน์ ์,์๋๋ก ์์ง์ฌ ๊ฐ์ ์๋ฆฌ๋ก ๋์์ฌ ์ ์์ด์ผํ๋ค. | ||
|
||
3. ์ด๋์ค์ ์์น์์ ์ต์ข ์์น๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๋จ์ ์์ง์์ผ๋ก ๊ฐ ์ ์์ ๋ | ||
|
||
์ด๋ ์ฌ์ ์์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์์ผํ๊ธฐ ๋๋ฌธ์ d(์๋), l(์ผ์ชฝ), r(์ค๋ฅธ์ชฝ), u(์์ชฝ) ์์ผ๋ก ์์ง์ด๋๋ก ๋์ดํ๊ณ ๊ฐ์ฅ ์ฒซ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ค. | ||
|
||
์ฒ์์๋ bfs๋ก ํ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ์ง๋ง, bfs๋ฅผ ์ฌ์ฉํ๋ฉด ์ต์ข ๋ชฉ์ ์ง๋ก ๋ฐ๋ก๊ฐ์ง์๊ณ ์ฃผ๋ณ์ ๋ชจ๋ ํ์ธํ๊ณ ์ต์ข ๋ชฉ์ ์ง๋ก ๊ฐ๊ธฐ ๋๋ฌธ์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค. | ||
|
||
dfs๋ฅผ ์ฌ์ฉํ์ฌ ์ต์ข ๋ชฉ์ ์ง๊น์ง ๋ฐ๋ก ๊ฐ ์ ์๋๋ก ์ฝ๋๋ฅผ ์ฌ๊ตฌํํ๋ค. ์ด๋ setrecursionlimit์ ํ์ง ์์ผ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ ์ ์๋ค. |
80 changes: 80 additions & 0 deletions
80
_posts/algorithm/๋น์ทํ-๋จ์ด/2023-10-22-์ฑ์นํฌ-๋น์ทํ-๋จ์ด.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: ๐น ๋น์ทํ ๋จ์ด | ||
author: chaeshee0908 | ||
date: 2023-10-22 20:00:00 +09:00 | ||
categories: [์๊ณ ๋ฆฌ์ฆ, ๋น์ทํ ๋จ์ด] | ||
tags: [์๊ณ ๋ฆฌ์ฆ, ๋ฐฑ์ค, ๊ณจ๋4, ๋ฌธ์์ด, 4์ฃผ์ฐจ, ์ฑ์นํฌ] | ||
render_with_liquid: false | ||
--- | ||
|
||
## 1. ๋ฌธ์ ๋งํฌ | ||
|
||
[2469๋ฒ: ๋น์ทํ ๋จ์ด](https://www.acmicpc.net/problem/2179) | ||
|
||
<br> | ||
|
||
## 2. ์ฝ๋ | ||
|
||
`Python3` `34072kb` `824ms` | ||
|
||
```python | ||
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 | ||
``` | ||
|
||
<br> | ||
|
||
## 3. ํด์ค | ||
|
||
๋จ์ด์ ๊ฐ์๊ฐ ์ต๋ 20000๊ฐ ์ด๋ฏ๋ก 2๊ฐ์ ๋จ์ด์ฉ ์ง์์ง์ด ๊ฐ ์ํ๋ฒณ์ ํ์ธํ์ฌ ์ ๋์ฌ๋ฅผ ์ฐพ๋ 3์ค ์ค์ฒฉ๋ฌธ์ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋๋ฏ๋ก ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํด๋ดค๋ค. | ||
|
||
๋ชจ๋ ๋จ์ด๋ฅผ ๊ณตํต์ ์ธ ๊ธธ์ด๋ก ์๋ผ ์ค๋ณต๋๋ ๋จ์ด๊ฐ ์๋ค๋ฉด ๊ทธ๊ฒ์ด ๊ฒน์น๋ ์ ๋์ฌ๊ฐ ๋ ๊ฒ์ด๊ณ ํด๋น ์ ๋์ฌ์ ๊ธธ์ด๊ฐ ๊ฐ์ฅ ๊ธธ๋๋ก ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค. ์ด๋ฅผ ์ํด ๋จ์ด์์ ๊ฐ์ฅ ๊ธด ๋จ์ด๋ฅผ ์ ๋์ฌ์ ์ต๋ ๊ธธ์ด๋ก ๋๊ณ ๋ฐ๋ณตํ ๋๋ง๋ค ๊ธธ์ด๋ฅผ 1์ฉ ์ค์ฌ๊ฐ๋ฉฐ ๊ฐ์ฅ ๊ธด ์ ๋์ฌ์ ๊ธธ์ด๋ฅผ ์ฐพ๋๋ค. | ||
|
||
check_prefix์์ ์ ๋ ฅ๋ ์ ๋์ฌ์ ๊ธธ์ด๋งํผ ๋จ์ด๋ฅผ ์๋ฅธ ํ ๋์ ๋๋ฆฌ๋ก ๋ง๋ค์ด ์ ๋์ฌ์ ๊ฐ์๊ฐ 1์ด ๋๊ฒ๋๋ ์ ๋์ฌ๊ฐ ๊ฐ์ฅ ๊ธด ์ ๋์ฌ๊ฐ ๋๋ค. ์ด๋ ์ด๋ฅผ ๋ง์กฑํ๋ ์ ๋์ฌ๊ฐ ์ฌ๋ฌ ๊ฐ ์ผ ์ ์์ผ๋ ๋ฐฐ์ด์ ๋ด์๋๋ค. | ||
|
||
์ ๋์ฌ๋ฅผ ์ฐพ๊ณ ๋ ํ ์ ๋ ฅ๋ฐ์ ์์๋๋ก ๋จ์ด๋ฅผ ํ์ธํ๊ณ ์ ๋์ฌ ๋ฐฐ์ด์ ํฌํจ๋ ์ ๋์ฌ๋ฅผ ๊ฐ์ง ๋จ์ด๋ฅผ ์ฐพ๊ณ ์ถ๋ ฅํ๋ค. ์ด๋ ์ด๋ค ์ ๋์ฌ๋ฅผ ์ฌ์ฉํ ์ง ํ์ ๋๋ค.(์ ๋์ฌ ๋ฐฐ์ด ์์์ ์ ๋ ฅ ์์๊ฐ ๊ฐ์ฅ ๋จ์ด์ ํฌํจ๋ ์ ๋์ฌ) | ||
|
||
๋ฐ๊ฒฌ๋ ์์น ๋ค์๋ถํฐ ์ ๋์ฌ๊ฐ ํฌํจ๋ ๊ฐ์ฅ ์ ์์ ๋จ์ด๋ฅผ ํ์ํ์ฌ ์ถ๋ ฅํ๋ค. |
Oops, something went wrong.