Submission #1022628

#TimeUsernameProblemLanguageResultExecution timeMemory
1022628subrat0018Type Printer (IOI08_printer)C++17
100 / 100
506 ms140476 KiB
#include <bits/stdc++.h> using namespace std; #define nline "\n" class Node{ public: bool word; vector<Node*> links; Node(){ word = false; links.resize(26, nullptr); } }; class Trie{ public: Node *root; Trie(){ root = new Node(); } void insert(string &s){ Node *node = root; for(auto &val: s){ if(!node->links[val - 'a']){ node->links[val - 'a'] = new Node(); } node = node->links[val - 'a']; } node->word = true; } }; vector<char> ans; unordered_map<Node *, int> height; void dfs(Node *n){ if(!n) return; height[n] = 1; for(int i=0;i<26;i++){ if(n->links[i]){ dfs(n->links[i]); height[n] = max(height[n], 1 + height[n->links[i]]); } } } Node *curr; bool cmp(int i, int j){ return height[curr->links[i]] < height[curr->links[j]]; } void dfs2(Node *n){ if(!n) return; if(n->word){ ans.push_back('P'); } int arr[26]; curr = n; iota(arr, arr+26, 0); sort(arr, arr+26,cmp); for(int i=0;i<26;i++){ if(!n->links[arr[i]])continue; ans.push_back(arr[i] + 'a'); dfs2(n->links[arr[i]]); ans.push_back('-'); } } void solve() { int n;cin>>n; Trie t; for(int i=0;i<n;i++){ string s; cin>>s; t.insert(s); } dfs(t.root); dfs2(t.root); while(ans.size() && ans.back() == '-')ans.pop_back(); cout<<ans.size()<<nline; for(auto &val: ans) cout<<val<<nline; } int main() { #ifndef ONLINE_JUDGE freopen("debug.txt", "w", stderr); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin>>t; // prec(); for (int i = 1; i <= t; i++) { // cout<<"Case "<<i<<": "; solve(); } return 0; }

Compilation message (stderr)

printer.cpp: In function 'int main()':
printer.cpp:84:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     freopen("debug.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...