# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1175260 | InvMOD | Type Printer (IOI08_printer) | C++20 | 161 ms | 147192 KiB |
#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
const int C = 26;
struct Trie{
struct Node{
Node* child[C]; bool has_end; int MxLen[C];
Node(): has_end(false) {
for(int i = 0; i < C; i++){
child[i] = nullptr;
MxLen[i] = 0;
}
}
};
typedef Node* pNode;
pNode root; vector<char> ans;
Trie() {
root = new Node();
}
void add(const string &s){
pNode cur = root;
for(const char &x : s){
int c = int(x - 'a');
if(cur->child[c] == nullptr){
cur->child[c] = new Node();
}
cur->MxLen[c] = max(cur->MxLen[c], sz(s));
cur = cur->child[c];
}
cur->has_end = true;
}
void find_answer(pNode x){
if(x->has_end) ans.push_back('P');
vector<int> id(26); iota(all(id), 0);
sort(all(id), [&](int u, int v){
return x->MxLen[u] < x->MxLen[v];
});
for(int i = 0; i < C; i++){
if(x->MxLen[id[i]]){
pNode v = x->child[id[i]];
ans.push_back(char('a' + id[i]));
find_answer(v);
}
}
ans.push_back('-');
}
void answer(){
find_answer(root);
while(ans.back() == '-') ans.pop_back();
cout << sz(ans) << "\n";
for(int i = 0; i < sz(ans); i++){
cout << ans[i] << "\n";
}
}
};
void solve()
{
int n; cin >> n;
Trie trie;
for(int i = 0; i < n; i++){
string s; cin >> s;
trie.add(s);
}
trie.answer();
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
return 0;
}
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... |