#include <bits/stdc++.h>
using namespace std;
const int ALPHABET_SIZE = 26;
class TrieNode {
public:
TrieNode* children[ALPHABET_SIZE];
bool isLeaf;
TrieNode() {
for (int i = 0; i < ALPHABET_SIZE; i++) {
children[i] = nullptr;
}
isLeaf = false;
}
void insert(string &word) {
for (char &c : word) {
if (children[c - 'a'] == nullptr)
children[c - 'a'] = new TrieNode();
children[c - 'a'];
}
}
};
class Trie {
private:
TrieNode* root;
public:
Trie() {
root = new TrieNode();
}
void insert(string &word) {
TrieNode* cur = root;
for (char &c : word) {
if (cur->children[c - 'a'] == nullptr)
cur->children[c - 'a'] = new TrieNode();
cur = cur->children[c - 'a'];
}
cur->isLeaf = true;
}
void traverse(string &longest, vector<char> &ans) {
dfs(root, longest, ans, 0);
}
void dfs(TrieNode* node, string &longest, vector<char> &ans, int pos) {
if (node == nullptr)
return;
if (node->isLeaf)
ans.push_back('P');
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (node->children[i] != nullptr && i + 'a' != longest[pos]) {
ans.push_back('a' + i);
dfs(node->children[i], longest, ans, pos);
ans.push_back('-');
}
}
if (node->children[longest[pos] - 'a']) {
ans.push_back(longest[pos]);
dfs(node->children[longest[pos] - 'a'], longest,
ans, pos + 1);
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<string> a(n);
string longest = "";
Trie* trie = new Trie();
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i].length() > longest.length())
longest = a[i];
trie->insert(a[i]);
}
vector<char> ans;
trie->traverse(longest, ans);
cout << ans.size() << "\n";
for (auto &c : ans) {
cout << c << "\n";
}
return 0;
}
Compilation message
printer.cpp: In member function 'void TrieNode::insert(std::string&)':
printer.cpp:22:29: warning: statement has no effect [-Wunused-value]
22 | children[c - 'a'];
| ~~~~~~~~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1876 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
6088 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
14936 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
37148 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
29164 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |