Submission #439451

#TimeUsernameProblemLanguageResultExecution timeMemory
439451ldn694Type Printer (IOI08_printer)C++17
100 / 100
129 ms52412 KiB
#include <bits/stdc++.h> #define fu(i, a, b) for (int i = a; i <= b; i++) #define fd(i, a, b) for (int i = a; i >= b; i--) using namespace std; const int N = 25e3 + 10; const int M = 5e5 + 10; int n; string a[N], res; int trie[M][27], sz = 0; bool tail[M]; string longest; void Insert(const string &a) { int node = 0; for (char c : a) { int x = int(c) - 96; if (!trie[node][x]) trie[node][x] = ++sz; node = trie[node][x]; } tail[node] = true; } void DFS(int u, int depth, bool still) { int c = int(longest[depth]) - 96; // cout << u << " " << depth << "\n"; fu(i, 1, 26) { if (trie[u][i] == 0) continue; if (i == c && still) continue; // cout << char(i + 96) << "\n"; res.push_back(char(i + 96)); if (tail[trie[u][i]]) { // cout << "P\n"; res.push_back('P'); } DFS(trie[u][i], depth + 1, false); // cout << "-\n"; res.push_back('-'); } if (still) { // cout << longest[depth] << "\n"; res.push_back(longest[depth]); if (tail[trie[u][c]]) res.push_back('P'); if (depth < (int)longest.size() - 1 && trie[u][c]) DFS(trie[u][c], depth + 1, still); } } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); cin >> n; fu(i, 1, n) { cin >> a[i]; } fu(i, 1, n) { Insert(a[i]); } longest = a[1]; fu(i, 2, n) { if (longest.size() < a[i].size()) longest = a[i]; } DFS(0, 0, 1); cout << (int)res.size() << "\n"; fu(i, 0, (int)res.size() - 1) { cout << res[i] << "\n"; } }
#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...