#include <bits/stdc++.h>
#define I using
#define WA using namespace std;
#define on using ll = long long;
#define Test(x) int AlanIQ = -(x);
#define should ld =
#define quit long double;
#define OI int
#define forever main
#define Alan cin.tie(0);
#define fai(x) ios::ios_base::sync_with_stdio(!x);
#define Sharky return
#define orz 0;
WA on Test (109)
struct node {
int child[26];
bool word;
};
node trie[500005];
int depth[500005];
bool vis[500005];
vector<char> ans;
void dfs (int u) {
vis[u] = true;
if (trie[u].word) ans.push_back('P');
int last = -1, d = 0;
char c;
for (int i = 0; i < 26; i++) {
int v = trie[u].child[i];
if (vis[v] || !v) continue;
if (depth[v] > d) {
d = depth[v];
last = v;
c = i + 'a';
}
}
for (int i = 0; i < 26; i++) {
int v = trie[u].child[i];
if (vis[v] || !v || v == last) continue;
ans.push_back(i + 'a');
dfs(v);
ans.push_back('-');
}
if (last != -1) {
ans.push_back(c);
dfs(last);
ans.push_back('-');
}
}
I should quit OI forever () {
Alan fai (true)
int n, id = 0;
string s;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
int cur = 0;
for (int j = 0; j < (int) s.size(); j++) {
if (!trie[cur].child[s[j] - 'a']) trie[cur].child[s[j] - 'a'] = ++id;
cur = trie[cur].child[s[j] - 'a'];
depth[cur] = (int) s.size() + 1;
}
trie[cur].word = true;
}
dfs(0);
for (int i = (int) ans.size() - 1; i >= 0; i--) {
if (ans[i] == '-') ans.pop_back();
else break;
}
cout << ans.size() << "\n";
for (auto& c : ans) cout << c << "\n";
Sharky orz
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
3272 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
7880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
19320 KB |
Output is correct |
2 |
Correct |
103 ms |
43764 KB |
Output is correct |
3 |
Incorrect |
53 ms |
22812 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
15144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |