# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
683907 | abcdehello | Type Printer (IOI08_printer) | C++17 | 1091 ms | 147844 KiB |
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;
struct node{
int ch;
bool print=false,lst=false;
vector<int> nxt;
};
node trie[1000050];
int n,nxt=1;
vector<string> ss,longest;
vector<char> ans;
void dfs(int u){
cerr << "trav " << u << endl;
if (u>0) ans.push_back(trie[u].ch);
if (trie[u].print) ans.push_back('P');
int lst=-1;
for (int i=0;i<26;i++){
if (trie[u].nxt[i]==-1) continue;
if (trie[trie[u].nxt[i]].lst){
lst=trie[u].nxt[i]; continue;
}
dfs(trie[u].nxt[i]);
}
if (lst>0) dfs(lst);
else ans.push_back('-');
}
bool comp(string a,string b){
if (a.length()!=b.length()) return a.length()<b.length();
return a<b;
}
int main(){
for (int i=0;i<1000050;i++){
trie[i].nxt.resize(26);
for (int j=0;j<26;j++){
trie[i].nxt[j]=-1;
}
}
cin >> n;
for (int i=0;i<n;i++){
string s;
cin >> s;
ss.push_back(s);
int cur=0;
for (int j=0;j<s.length();j++){
if (trie[cur].nxt[s[j]-'a']==-1){
trie[cur].nxt[s[j]-'a']=nxt++;
trie[nxt-1].ch=s[j];
}
cur=trie[cur].nxt[s[j]-'a'];
}
trie[cur].print=1;
}
sort(ss.begin(),ss.end(),comp);
string lst=ss.back();
int cur=0;
for (int i=0;i<lst.length();i++){
cur=trie[cur].nxt[lst[i]-'a'];
trie[cur].lst=1;
}
cerr << endl;
dfs(0);
ans.pop_back();
cout << ans.size() << "\n";
for (auto i:ans) cout << i << "\n";
}
Compilation message (stderr)
# | 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... |