☑️ 문제
https://www.acmicpc.net/problem/5670
휴대폰에서 길이가 P인 영단어를 입력하려면 버튼을 P번 눌러야 한다. 그러나 시스템프로그래밍 연구실에 근무하는 승혁연구원은 사전을 사용해 이 입력을 더 빨리 할 수 있는 자판 모듈을 개발하였다. 이 모듈은 사전 내에서 가능한 다음 글자가 하나뿐이라면 그 글자를 버튼 입력 없이 자동으로 입력해 준다! 자세한 작동 과정을 설명하자면 다음과 같다.
- 모듈이 단어의 첫 번째 글자를 추론하지는 않는다. 즉, 사전의 모든 단어가 같은 알파벳으로 시작하더라도 반드시 첫 글자는 사용자가 버튼을 눌러 입력해야 한다.
- 길이가 1 이상인 문자열 c1c2...cn이 지금까지 입력되었을 때, 사전 안의 모든 c1c2...cn으로 시작하는 단어가 c1c2...cnc로도 시작하는 글자 c가 존재한다면 모듈은 사용자의 버튼 입력 없이도 자동으로 c를 입력해 준다. 그렇지 않다면 사용자의 입력을 기다린다.
예를 들면, 사전에 "hello", "hell", "heaven", "goodbye" 4개의 단어가 있고 사용자가 "h"를 입력하면 모듈은 자동으로 "e"를 입력해 준다. 사전상의 "h"로 시작하는 단어들은 모두 그 뒤에 "e"가 오기 때문이다. 그러나 단어들 중 "hel"로 시작하는 것도, "hea"로 시작하는 것도 있기 때문에 여기서 모듈은 사용자의 입력을 기다린다. 이어서 사용자가 "l"을 입력하면 모듈은 "l"을 자동으로 입력해 준다. 그러나 여기서 끝나는 단어인 "hell"과 그렇지 않은 단어인 "hello"가 공존하므로 모듈은 다시 입력을 기다린다. 사용자가 "hell"을 입력하기 원한다면 여기서 입력을 멈출 것이고, "hello"를 입력하기 원한다면 직접 "o" 버튼을 눌러서 입력해 줘야 한다. 따라서 "hello"를 입력하려면 사용자는 총 3번 버튼을 눌러야 하고, "hell", "heaven"은 2번이다. "heaven"의 경우 "he"에서 "a"를 입력하면 바로 뒷부분이 모두 자동으로 입력되기 때문이다. 비슷하게, "goodbye"는 단 한 번만 버튼을 눌러도 입력이 완료될 것이다. "g"를 입력하는 순간 뒤에 오는 글자가 항상 유일하므로 끝까지 자동으로 입력되기 때문이다. 이때 사전에 있는 단어들을 입력하기 위해 버튼을 눌러야 하는 횟수의 평균은 (3 + 2 + 2 + 1)/4 = 2.00이다.
사전이 주어졌을 때, 이 모듈을 사용하면서 이와 같이 각 단어를 입력하기 위해 버튼을 눌러야 하는 횟수의 평균을 구하는 프로그램을 작성하시오.
입력
입력은 여러 개의 테스트 케이스로 이루어져 있다.
각 테스트 케이스의 첫째 줄에 사전에 속한 단어의 개수 N이 주어지며(1 ≤ N ≤ 10^5), 이어서 N개의 줄에 1~80글자인 영어 소문자로만 이루어진 단어가 하나씩 주어진다. 이 단어들로 사전이 구성되어 있으며, 똑같은 단어는 두 번 주어지지 않는다. 각 테스트 케이스마다 입력으로 주어지는 단어의 길이 총합은 최대 10^6이다.
출력
각 테스트 케이스마다 한 줄에 걸쳐 문제의 정답을 소수점 둘째 자리까지 반올림하여 출력한다.
예제 입력1
4
hello
hell
heaven
goodbye
3
hi
he
h
7
structure
structures
ride
riders
stress
solstice
ridiculous
예제 출력1
2.00
1.67
2.71
☄️ 정답코드
import java.io.*;
import java.util.*;
public class Main {
static class TrieNode{
Map<Character, TrieNode> childNode = new HashMap<>();
boolean terminal = false;
int childCount = 0;
TrieNode(){}
public void insert(String word) {
TrieNode trieNode = this;
for(int i=0; i<word.length(); i++) {
char c = word.charAt(i);
if (!trieNode.childNode.containsKey(c)) {
trieNode.childNode.put(c, new TrieNode());
trieNode.childCount++;
}
trieNode = trieNode.childNode.get(c);
}
trieNode.terminal = true;
}
public int countKeyPresses(String word) {
TrieNode trieNode = this;
int pressCount = 1;
for(int i=0; i<word.length(); i++) {
char c = word.charAt(i);
TrieNode node = trieNode.childNode.get(c);
if(i > 0) {
if (trieNode.terminal || trieNode.childCount > 1) {
pressCount++;
}
}
trieNode = node;
}
return pressCount;
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine())!=null) {
int N = Integer.parseInt(line);
List<String> inputData = new ArrayList<>();
TrieNode trie = new TrieNode();
for(int i=0; i<N; i++) {
String str = br.readLine();
inputData.add(str);
trie.insert(str);
}
double res = 0;
for(String str : inputData) {
res += trie.countKeyPresses(str);
}
System.out.printf("%.2f%n", res/N);
}
}
}
🪐 풀이
트라이
✅ TrieNode
Map<Character, TrieNode> childNode = new HashMap<>();
boolean terminal = false; // 단어의 끝인지
int childCount = 0; // 자식의 개수
✅ insert
재귀적으로 글자 하나하나 트리의 노드를 만들어간다!
public void insert(String word) {
TrieNode trieNode = this;
for(int i=0; i<word.length(); i++) {
char c = word.charAt(i);
if (!trieNode.childNode.containsKey(c)) {
trieNode.childNode.put(c, new TrieNode());
trieNode.childCount++;
}
trieNode = trieNode.childNode.get(c);
}
trieNode.terminal = true;
}
✅ countKeyPresses
public int countKeyPresses(String word) {
TrieNode trieNode = this;
int pressCount = 1; // 첫 글자는 무조건 입력해야 하므로
for(int i=0; i<word.length(); i++) {
char c = word.charAt(i);
TrieNode node = trieNode.childNode.get(c);
if(i > 0) {
if (trieNode.terminal || trieNode.childCount > 1) {
pressCount++;
}
}
trieNode = node;
}
return pressCount;
}
* 예제에서의 hell과 hello를 기준으로 어떻게 동작하는지 살펴보자!
hell
i | 입력문자 | trieNode | trieNode.terminal | trieNode.childCount | pressCount |
0 | h | root | ❌ | 1 | 1 (초기값) |
1 | e | h | ❌ | 1 | ❌ |
2 | l | e | ❌ | 2 | 2 (증가) |
3 | l | l | ❌ | 1 | ❌ |
hello
i | 입력문자 | trieNode | trieNode.terminal | trieNode.childCount | pressCount |
0 | h | root | ❌ | 1 | 1 (초기값) |
1 | e | h | ❌ | 1 | ❌ |
2 | l | e | ❌ | 2 | 2 (증가) |
3 | l | l | ❌ | 1 | ❌ |
4 | o | l | ✅ | 1 | 3(증가) |
'알고리즘 > 백준' 카테고리의 다른 글
[Java | DP | 20303번 | 골2] 할로윈의 양아치 (1) | 2025.03.04 |
---|---|
[Java | 세그먼트 트리| 1395번 | 플3] 스위치 (0) | 2025.02.27 |
[Java | 시뮬레이션 | 14939번 | 플4] 불 끄기 (0) | 2025.02.26 |
[Java | 투포인터 | 25395번 | 골3] 커넥티드 카 실험 (1) | 2025.02.23 |
[Java | DP | 2342번 | 골3] Dance Dance Revolution (1) | 2025.02.09 |