-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4-jung0115 #134
4-jung0115 #134
Conversation
์ด ๋ฌธ์ AlgoLeadMe/AlgoLeadMe-1#155 (comment) ๋ ๋งค์ฐ ๋น์ทํ๋ฐ, ์ด๋ฌํ ํธ๋ฆฌ ๋ชจ์์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํธ๋ผ์ด๋ผ๊ณ ํ๋๋ฐ ๋ฐ๋ก ํธ๋ฆฌ๋ก ์ด๋ ๊ฒ ๋ฐ๋ฐ๋ฐใ ๋ฝ ํ์ ๊ฑฐ ๋๋ฐ ๋๋ํ์๋ค์ ................................. ๋ก์ง๋ ์์ ํธ๋ผ์ด๋ ๋์ผํ๊ตฌ ๋๋ฐ .. ์ด ๋ฌธ์ ํธ์ จ์ผ๋ฉด https://www.acmicpc.net/problem/7432 ์๋ ๊ณต์ง์์! |
ํ ๋ฌธ์์ด ์ ์ฅ/๊ด๋ฆฌํ๋ ํธ๋ฆฌ๊ฐ ํธ๋ผ์ด๊ตฐ์...! ์ด ๋ฌธ์ ๋ ํ ๋ฒ ํ์ด๋ณด๊ณ ํธ๋ผ์ด ๊ฐ๋ ์ ํ์คํ ์ดํดํ๊ณ ๋์ด๊ฐ์ผ๊ฒ ์ต๋๋ค ๐ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ด ๋ฌธ์ ๋ฅผ ์ ๋ ฌํ ํ์ ์์ ํ์์ ํ๋ ๋ก์ง์ผ๋ก ํ์์ต๋๋ค!
์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ๋ ํ, ์ฃผ์ด์ง ๋จน์ด ์ ๋ณด์์ ์ค๋ณต๋๋ ์ฒซ ๋ฒ์งธ ์์๋ ์๋ตํ๊ณ ์ถ๋ ฅํ๋๋ก ๊ตฌํํ๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋์ ์์ ๋
ธ๋์ ์ค๋ณต ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๋ก์ง๋ ํจ๊ป ์ถ๊ฐํด์ฃผ์์ต๋๋ค.
import sys
input = sys.stdin.readline
# ์
๋ ฅ ์ฒ๋ฆฌ
M = int(input())
ant_nest = []
for _ in range(M):
elements = list(input().split())
ant_nest.append(elements[1:])
# ์ฌ์ ์์ผ๋ก ์ ๋ ฌ
ant_nest.sort()
separator = '--'
output = []
# ํธ๋ฆฌ ๊ตฌ์กฐ ์ถ๋ ฅ
for i in range(M):
if i == 0:
for j in range(len(ant_nest[i])):
output.append(separator * j + ant_nest[i][j])
else:
common_prefix_len = 0
for j in range(len(ant_nest[i])):
if len(ant_nest[i - 1]) <= j or ant_nest[i - 1][j] != ant_nest[i][j]:
break
else:
common_prefix_len = j + 1
for j in range(common_prefix_len, len(ant_nest[i])):
output.append(separator * j + ant_nest[i][j])
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
for line in output:
print(line)
์ด๋ฌํ ํธ๋ฆฌ ๋ชจ์์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํธ๋ผ์ด๋ผ๊ณ ํ๋๊ตฐ์!
ํ๊ท๋ ๋๋ถ์ ์๋ก์ด ๊ฐ๋
์ ์ ํ์ต๋๋น ๊ฐ์ฌํด์๐
์ด ๋ถ๋ถ์ ๋ํด์ ์ข ๋ ์์ธํ ๊ณต๋ถํด๋ด์ผ๊ฒ ์ต๋๋ค!
์ด๋ฒ PR๋ ์๊ณ ํ์ จ์ด์๐๐
ํ์์์น ํ์ด๋ง ํ์ด๋ง ์ด์ฌํ ํด์ ์ด์ ์์ธ ์ฌ๋ผ์ค์ผ~ |
๋ฌธ์์ด์ ์ ์ฅํ๊ณ ํจ์จ์ ์ผ๋ก ํ์ํ๊ธฐ ์ํ ํธ๋ฆฌ ํํ์ ์๋ฃ๊ตฌ์กฐ์ธ ํธ๋ผ์ด(Trie)์ ๋ํด ์ฒ์ ์๊ฒ ๋์์ต๋๋ค .... !! ์ ๋ง ๋๋จํ์ญ๋๋ค ๋ค๋ค ... ใ ใ ๋ฌธ์์ด์ ์ ๋์ฌ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฃจํธ ๋ ธ๋๊ฐ ๋น์ด์๊ณ , ์ฌ๋ฌ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง๋ฉฐ, ๋ฌธ์์ด์ ๋์ ํ์ํ๋ ์ฒ๋ฆฌ๋ฅผ ํ๋ค๋ ์ ๊น์ง ๊ณต๋ถํด๋ณผ ์ ์์์ต๋๋ค ! ์ ๋ ํธ๋ฆฌ ๋ฌธ์ ํ๋ฉด์ ์ด๋ฐ ๋ฌธ์ ์ ํ์ ํ๋ฒ ๋์ ํด๋ด์ผ๊ฒ ์ต๋๋ค :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ๋ก์ ํธ๊ฐ ์ด๊ฒ์ ๊ฒ ๋ฐ๋น ์ ์ด์ ์ผ ๋ฆฌ๋ทฐ๋ฅผ ๋๋ฆฌ๋ค์ใ ใ ๐
์ ๋ ๋ฐ๋ก ๋์ ํด ๋ณด์๋๋ฐ์,,!! ์ด ๋ฌธ์ ์์๋ ๊ฐ์ฒด๊ฐ ์ ์ฅ๋ ๋ key๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ํด์ผ ํ๋ฏ๋ก, TreeMap
์ ์ฑํํ์ต๋๋ค! treeMap์ ๋ด๋ถ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋, Comparator๋ฅผ ๊ตฌํํ๊ณ ์์ด key๊ฐ์ ์ ๋ ฌํด ์ฃผ๊ธฐ ๋๋ฌธ์ ์ด ๋ฌธ์ ์ ๋ฑ ์ ํฉํ๋ค๋ ์๊ฐ์ด ๋ค์์ด์ :)
import java.io.*;
import java.util.*;
public class Main {
static StringBuilder result = new StringBuilder();
static TreeMap<String, TreeMap> treeMap = new TreeMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int k;
for (int i = 0; i < n; i++) {
String[] inputs = br.readLine().split(" ");
k = Integer.parseInt(inputs[0]);
TreeMap currentFoodInfo = treeMap;
for (int j = 0; j < k; j++) {
String food = inputs[j + 1];
if (!currentFoodInfo.containsKey(food)) {
currentFoodInfo.put(food, new TreeMap<>());
}
currentFoodInfo = (TreeMap)currentFoodInfo.get(food);
}
}
draw(treeMap, 0);
System.out.println(result);
}
private static void draw(TreeMap<String, TreeMap> treeMap, int nodeCount) {
for (Object food : treeMap.keySet()) {
for (int i = 0; i < nodeCount; i++) {
result.append("--");
}
result.append(food).append("\n");
draw(treeMap.get(food), nodeCount + 1);
}
}
}
์๋ง ํ์๋ฉด์ ์๊ณ ๋ฆฌ์ฆ ์ ๋ฐ๋ฆฌ๊ตฌ ์ฑ์คํ ํ์๋ ๊ฒ ๋๋จํ์ ๊ฒ ๊ฐ์์,, ์ ๋ ๋์ฑ ๋ถ๋ฐํ๊ฒ ์ต๋๋ค ใ ใ
[๐ฅ ํ์ด ์ฝ๋]
๐ ๋ฌธ์ ๋งํฌ
๋ฐฑ์ค - ์๋ฃ ๊ตฌ์กฐ | ๊ฐ๋ฏธ๊ตด(14725)
โ๏ธ ์์๋ ์๊ฐ
35๋ถ
โจ ์๋ ์ฝ๋
๋ฌธ์ ์ ์ ์๋ ๊ทธ๋ฆผ์ด ๋๋ฌด ํธ๋ฆฌ์ ํํ๋ฅผ ๋ํ๋ด๊ณ ์์ด์ ๋ฐ๋ก ํธ๋ฆฌ๋ฅผ ํ์ฉํด์ผ๊ฒ ๋ค๋ ์๊ฐ์ ํ์ต๋๋ค ๐
๋ค๋ง ์ด์ง ํธ๋ฆฌ๊ฐ ์๋๋ผ ์์ ๋ ธ๋๋ก ์ฌ๋ฌ๊ฐ๊ฐ ์ฌ ์ ์๋ค๋ ์ , ๊ทธ๋ฆฌ๊ณ ์์ ๋ ธ๋๋ฅผ ๋จน์ด ์ํ๋ฒณ ์์ผ๋ก ๋ณด์ฌ์ค์ผ ํ๋ค๋ ์ ์ ์ฃผ์ํ์ต๋๋ค!
์์์ผ๋ก ์ฌ๋ฌ๊ฐ๊ฐ ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ์์์ list๋ก ๊ด๋ฆฌํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ์ต๋๋ค
๊ทธ๋์ ์ด๋ ๊ฒ ๋ ธ๋ ์ ๋ณด์ ๋จน์ด ์ด๋ฆ๊ณผ ์์์ ์ ์ฅํ ๋ฆฌ์คํธ๋ฅผ ์ค์ ํด์คฌ์ต๋๋ค
๋ฌธ์ ์ ์ ์๋ ์์ ๋ก ์ค๋ช ์ ํ์๋ฉด, ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ ์ฅ์ด ๋๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค
๐ค ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ก ์ ๋ฆฌํ ์ ์์๊น์?
์ ๋ ๊ฐ๋ฏธ๊ฐ ์ง๋์จ ๊ฒฝ๋ก์ ์ ๋ ฅ ์ ๋ณด๋ง๋ค ์ ๊ณผ์ ์ ๋ฐ๋ณตํด์ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ์ ๋ฆฌ๋ฅผ ํด์คฌ์ต๋๋ค. ์ต์์์ ๊ฐ๋ฏธ๊ตด ์ ๊ตฌ๋ฅผ root๋ก ๋๊ณ , ์ ๋ ฅ ์์๋๋ก ์์ ๋ ธ๋๋ฅผ ์ถ๊ฐํด์ค ๊ฒ์ ๋๋ค!
Tree๋ก ์ ๋ฆฌ๋ ์ ๋ณด๋ฅผ ์ถ๋ ฅ ํ์์ ๋ง์ถฐ์ ์ ๋ฆฌ
๊ฐ๋ฏธ๊ตด ์ ๊ตฌ, ์ฆ root ๋ ธ๋ ์ ๋ณด๋ ์ถ๋ ฅํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ทธ ์๋ ๋ ธ๋๋ถํฐ ํ์์ ํด์ค๋๋ค. ๋จ, ์ํ๋ฒณ ์์ผ๋ก ์ถ๋ ฅ์ ํด์ผํ๊ธฐ ๋๋ฌธ์ ์์ ๋ ธ๋๋ฅผ ์ ๋ ฌํ๊ณ , ์ ์์ํ ๋ฐฉ์ํ๋ฉด์์ถ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฆฌํ ์ ์์์ต๋๋ค
โ ์ต์ข ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ps. ์ด๋ฒ ์ฐจ์์๋ ๋ค๋ฅธ ์ผ์ ๋๋ฌธ์ ๋ฌธ์ ๋ฅผ ํ ์๊ฐ์ด ๋ถ์กฑํด์ ์ ๋ต๋ฅ ๋์ ๋ฌธ์ ๋ฅผ ์ ํํ๋๋ฐ, ๋ค์ ๋ฌธ์ ๋ ์ข ๋ ์๊ฐ๊ณผ ๊ณ ๋ฏผ์ด ๋ง์ด ํ์ํ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด๋ ค๊ณ ํฉ๋๋ค!