#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
const int MAX_N = 5e5 + 5;
const int ALPHA = 26;
struct Node {
int child[ALPHA];
bool isEnd = false, isLongest = false;
};
Node trie[MAX_N];
int trieSize = 0;
void insert(string s, bool longest) {
int u = 0;
for (char c : s) {
int &v = trie[u].child[c - 'a'];
if (v == 0) v = ++trieSize;
u = v;
if (longest) trie[u].isLongest = true;
}
trie[u].isEnd = true;
}
string ans = "";
void dfs(int u) {
if (trie[u].isEnd) ans += 'P';
int j = -1;
for (int i = 0; i < ALPHA; i++) {
int v = trie[u].child[i];
if (v == 0) continue;
if (trie[v].isLongest) {
j = i;
} else {
ans += char('a' + i);
dfs(v);
}
}
if (j != -1) {
ans += char('a' + j);
int v = trie[u].child[j];
dfs(v);
}
if (u != 0 && !trie[u].isLongest) ans += '-';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<string> word(n);
int j = 0;
for (int i = 0; i < n; i++) {
cin >> word[i];
if (sz(word[i]) > sz(word[j])) {
j = i;
}
}
for (int i = 0; i < n; i++) {
insert(word[i], i == j);
}
dfs(0);
cout << sz(ans) << '\n';
for (char i : ans) {
cout << i << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
53080 KB |
Output is correct |
2 |
Correct |
7 ms |
53084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
53080 KB |
Output is correct |
2 |
Correct |
8 ms |
53496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
53084 KB |
Output is correct |
2 |
Correct |
7 ms |
53084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
53112 KB |
Output is correct |
2 |
Correct |
8 ms |
53080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
53080 KB |
Output is correct |
2 |
Correct |
7 ms |
53340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
53340 KB |
Output is correct |
2 |
Correct |
11 ms |
53340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
53596 KB |
Output is correct |
2 |
Correct |
16 ms |
53848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
54224 KB |
Output is correct |
2 |
Correct |
13 ms |
54108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
55280 KB |
Output is correct |
2 |
Correct |
67 ms |
57616 KB |
Output is correct |
3 |
Correct |
34 ms |
56560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
55024 KB |
Output is correct |
2 |
Correct |
66 ms |
58380 KB |
Output is correct |
3 |
Correct |
41 ms |
57068 KB |
Output is correct |
4 |
Correct |
58 ms |
58376 KB |
Output is correct |