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>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
typedef struct Node {
int adj[26], par; bool end;
Node(int p = -1) {
end = 0; par = p;
fill(adj, adj + 26, -1);
}
} node;
vector<node> tree(1);
int add(string &s) {
int cur = 0;
for (char c : s) {
if (tree[cur].adj[c - 'a'] == -1) {
tree[cur].adj[c - 'a'] = sz(tree);
tree.emplace_back(cur);
}
cur = tree[cur].adj[c - 'a'];
}
tree[cur].end = true;
return cur;
}
int cnt = 0, N;
vector<string> res;
vector<int> mx;
void dfs(int u) {
if (tree[u].end) {
res.pb("P");
cnt++;
}
vector<vector<int>> srt;
for (int i = 0; i < 26; i++) {
if (tree[u].adj[i] != -1) {
srt.pb({mx[tree[u].adj[i]], i});
}
}
sort(all(srt));
for (vector<int> v : srt) {
int i = v[1];
string s = ""; s += (char)(i + 'a');
res.pb(s);
dfs(tree[u].adj[i]);
if (cnt < N) res.pb("-");
}
}
void solve() {
cin >> N;
for (int i = 0; i < N; i++) {
string s; cin >> s;
add(s);
}
mx = vector<int>(sz(tree));
fill(all(mx), 0);
for (int i = sz(tree) - 1; i > 0; i--) {
mx[tree[i].par] = max(mx[tree[i].par], mx[i] + 1);
}
dfs(0);
cout << sz(res) << "\n";
for (string s : res) {
cout << s << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
# | 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... |