Submission #935210

#TimeUsernameProblemLanguageResultExecution timeMemory
935210asdasdqwerType Printer (IOI08_printer)C++14
20 / 100
380 ms225520 KiB
#include <bits/stdc++.h> using namespace std; struct Node { char l; map<char,int> child; }; vector<Node> trie(500001); int last = 1; void insWord(string &s) { int idx = 0; for (auto &x:s) { if (trie[idx].child[x-'a'] == 0) { trie[idx].child[x-'a'] = last; trie[last].l = x; last++; } idx = trie[idx].child[x-'a']; } } struct Operation { char chr; bool isPrint = false; bool isBack = false; }; vector<Operation> out; void traversal(int index) { bool foundOne = false; for (int i=0;i<26;i++) { if (trie[index].child[i] == 0) continue; Operation op;op.chr = i + 'a'; out.push_back(op); traversal(trie[index].child[i]); op.isBack = true; out.push_back(op); foundOne = true; } if (!foundOne) { Operation op;op.isPrint=true; out.push_back(op); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n;cin>>n; vector<string> v(n); for (auto &x:v)cin>>x; trie[0].l = ' '; for (auto x:v) { insWord(x); } // it's always optimal to stop at the word with the largest length string s = ""; for (auto &x:v) { if (x.size() > s.size()) { s = x; } } int index = 0; for (auto &x:s) { int nxt; for (int i=0;i<26;i++) { if (trie[index].child[i] != 0 && i != (x-'a')) { Operation op;op.chr=i+'a'; out.push_back(op); traversal(trie[index].child[i]); op.isBack = true; out.push_back(op); } else if (i == (x-'a')) { nxt = trie[index].child[i]; } } Operation op;op.chr=x; out.push_back(op); index = nxt; } Operation op;op.isPrint=true; out.push_back(op); cout<<out.size()<<"\n"; for (auto &x:out) { if (x.isBack) { cout<<"-\n"; } else if (x.isPrint) { cout<<"P\n"; } else { cout<<x.chr<<"\n"; } } }

Compilation message (stderr)

printer.cpp: In function 'int main()':
printer.cpp:75:27: warning: 'nxt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |             if (trie[index].child[i] != 0 && i != (x-'a')) {
      |                           ^
#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...