https://www.acmicpc.net/problem/3080
3080번: 아름다운 이름
상근 선생님은 학생들에게 번호를 붙여주려고 한다. 상근이는 미술 선생님이기 때문에, 이름의 순서도 아름다워야 한다고 생각한다. 따라서, 다음과 같은 규칙을 지켜서 번호를 정하려고 한다.
www.acmicpc.net
1.문제설명
입력받는 이름들로 트라이 구조를 만들고
분기점과 문자열이 끝나는 점을 바탕으로
순열 계산을 하면 학생들을 배치할 수 있는 경우의 수를 구해낼 수 있다.
2.문제풀이코드 C++
|
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
|
//package org.example;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
final int MX = 26;
final long mod = 1000000007;
public long factorial(int n) {
long ret = 1;
for (int i = 1; i <= n; i++) {
ret *= i;
ret %= mod;
}
return ret;
}
class Trie {
Trie[] child;
int childNum;
int end;
public Trie() {
child = new Trie[MX];
Arrays.fill(child, null);
childNum = end = 0;
}
public void insert(String str) {
Trie cur = this;
for (int i = 0; i < str.length(); i++) {
int ch = str.charAt(i) - 'A';
if (cur.child[ch] == null) {
cur.child[ch] = new Trie();
cur.childNum++;
}
cur = cur.child[ch];
}
cur.end = 1;
}
public long getAns() {
long ret = factorial(this.childNum + this.end);
for (Trie nx : this.child) {
if(nx==null) continue;
ret *= nx.getAns();
ret %= mod;
}
return ret % mod;
}
}
public void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Trie root = new Trie();
for (int i = 0; i < n; i++) {
String str = br.readLine();
root.insert(str);
}
System.out.println(root.getAns());
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}
|
cs |
'Algorithm > BOJ' 카테고리의 다른 글
| 백준 3015번 : 오아시스 재결합 Java (0) | 2023.02.11 |
|---|---|
| 백준 9250번 : 문자열 집합 판별 - 아호코라식 Java (0) | 2023.01.06 |
| 백준 5446번 : 용량 부족 - 트라이, Java (0) | 2023.01.02 |
| 백준 4358번 : 생태학 - hash, Java (0) | 2022.12.31 |
| 백준 9202번 : Boggle - 트라이 Java (0) | 2022.12.31 |