Submission #780168

#TimeUsernameProblemLanguageResultExecution timeMemory
780168raysh07Type Printer (IOI08_printer)C++17
100 / 100
180 ms113336 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...