This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
int n, ind = 1;
std::vector <char> ans;
std::vector <int> par(5e5);
std::vector <std::vector <int>> tr(5e5, std::vector <int> (26, -1));
std::vector <bool> vis(5e5, false), tak(5e5, false);
int add(const std::string& s) {
int r = 0;
for (const char& c : s) {
if (tr[r][c - 'a'] == -1) tr[r][c - 'a'] = ind, par[ind++] = r;
r = tr[r][c - 'a'];
}
vis[r] = true;
return r;
}
void dfs(int now = 0) {
if (vis[now]) ans.push_back('P');
char ch;
int other = -1;
for (int i = 0; i < 26; i++) {
if (tr[now][i] == -1) continue;
char c = i + 'a';
if (tak[tr[now][i]]) ch = c, other = tr[now][i];
else ans.push_back(c), dfs(tr[now][i]), ans.push_back('-');
}
if (other != -1) ans.push_back(ch), dfs(other);
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
std::cin >> n;
int l = 0, node = -1;
par[0] = -1;
for (int i = 0; i < n; i++) {
std::string s; std::cin >> s;
int now = add(s);
if ((int)s.length() > l) l = s.length(), node = now;
}
while ((node) >= 0) tak[node] = true, node = par[node];
dfs();
std::cout << ans.size() << "\n";
for (char c : ans) std::cout << c << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |