Submission #1022628

# Submission time Handle Problem Language Result Execution time Memory
1022628 2024-07-13T19:42:43 Z subrat0018 Type Printer (IOI08_printer) C++17
100 / 100
506 ms 140476 KB
#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

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 time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 4 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2392 KB Output is correct
2 Correct 9 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 8368 KB Output is correct
2 Correct 62 ms 17356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 20668 KB Output is correct
2 Correct 17 ms 4580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 50888 KB Output is correct
2 Correct 454 ms 119036 KB Output is correct
3 Correct 211 ms 61096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 40304 KB Output is correct
2 Correct 506 ms 140476 KB Output is correct
3 Correct 234 ms 68940 KB Output is correct
4 Correct 453 ms 132888 KB Output is correct