#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];
bool last[500005], vis[500005];
vector<char> ans;
int lastd = 0;
void dfs (int u) {
vis[u] = true;
if (trie[u].word) ans.push_back('P');
char c;
int okkkookokokokoknotok = -1;
for (int i = 0; i < 26; i++) {
int v = trie[u].child[i];
if (vis[v] || !v) continue;
if (last[v]) {
okkkookokokokoknotok = v;
continue;
}
ans.push_back(i + 'a');
dfs(v);
ans.push_back('-');
}
if (okkkookokokokoknotok != -1) {
ans.push_back(c);
dfs(okkkookokokokoknotok);
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;
int sz = (int) s.size();
bool ishouldquitoi = false;
if (sz > lastd) {
lastd = sz;
ishouldquitoi = true;
}
for (int j = 0; j < sz; j++) {
if (!trie[cur].child[s[j] - 'a']) trie[cur].child[s[j] - 'a'] = ++id;
cur = trie[cur].child[s[j] - 'a'];
if (ishouldquitoi) last[cur] = true;
}
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 |
Incorrect |
0 ms |
332 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1100 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
3148 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
7560 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
18440 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
14448 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |