Submission #1324844

#TimeUsernameProblemLanguageResultExecution timeMemory
1324844z3farType Printer (IOI08_printer)C++20
100 / 100
95 ms60040 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;


const int mod1 = 1e9+7 , mod2 = 1e9+9;

const int b1 = 437, b2 = 537;
const int alpha = 26;
const int zero = 'a';

struct PairOfHash {
    int hash1;
    int hash2;

    bool operator==(const PairOfHash &other) const {
        return this->hash1 == other.hash1 && this->hash2 == other.hash2;
    }

};
const int N = 1e6 +5;

int pw1[N] , pw2[N];

void precompute() {
    pw1[0] = pw2[0] = 1;

    for (int i = 1 ; i < N ; i++) {
        pw1[i] = (1ll * pw1[i-1]* b1)%mod1;
        pw2[i] = (1ll * pw2[i-1]* b2)%mod2;
    }
}
struct HashedString {
    vector<int> hash1;
    vector<int> hash2;
    int sz;
    HashedString(const string s) {
        sz = s.size();
        hash1 = vector<int> (sz+1);
        hash2 = vector<int> (sz+1);

        for (int i = 0 ; i< sz; i++) {
            hash1[i+1] = (1ll * hash1[i] * b1 + s[i])%mod1;
            hash2[i+1] = (1ll * hash2[i] * b2 + s[i])%mod2;
        }
    }

    PairOfHash query(const int s , const int e) {
        int h1 = (hash1[e+1] - (1ll*hash1[s]*pw1[e-s+1])%mod1 + mod1 ) %mod1;
        int h2 = (hash2[e+1] - (1ll*hash2[s]*pw2[e-s+1])%mod2 + mod2 ) %mod2;
        return {h1 , h2};
    }
};


// int fq[N];
struct Node {
    int f =0;
    int e =0;
    int arr[alpha]={};
    int depth=0;

    bool operator<(const Node &other) const {
        return this->depth < other.depth;
    }
};
struct Trie {
    vector<Node> trie;
    vector<char> ans;

    Trie() {
        trie.emplace_back();
    }
    void insert(const string s) {
        int node = 0;
        vector<int> nodes;
        const int n = s.size();
        for (auto c :s) {
            int ch = trie[node].arr[c -zero] ;// child index in the trie;
            if (trie[ch].f == 0) { // removed or not created
                if (ch == 0) //not created
                {
                    ch = trie.size();
                    trie.emplace_back();
                }
                trie[node].arr[c -zero] =ch;
            }
            node = ch;
            nodes.push_back(node);
            trie[node].f++;
        }
        trie[node].e++;
        for (int i = 0 ; i < n; i++) {
            trie[nodes[i]].depth = max( trie[nodes[i]].depth , n-i);
        }
    }

    void remove(const string s) {
        int node = 0;
        for (auto c :s) {
            int const ch = trie[node].arr[c -zero] ;
            if (trie[ch].f == 0) {
                return;
            }
            node = ch;
            trie[node].f--;
        }
        trie[node].e--;
    }

    void query () {
        dfs(0);

    }

    void dfs(int node ) {

        while (trie[node].e --) {
            ans.push_back('P');
        }
        vector<pair<int,pair<int,int>>> v;
        for (int i = 0 ; i<26 ; i++) {
            const int c = trie[node].arr[i];
            if (c) {
                v.push_back({trie[c].depth , {c,i}});
            }
        }
        sort(v.begin(), v.end());
        for (auto [depth , n] : v) {
            ans.push_back(n.second+zero);
            dfs(n.first);
        }
        ans.push_back('-');
    }
};

void solve() {
    int n;
    cin>>n;
    Trie tr  = Trie();
    string s;
    while (n--) {
        cin>>s;
        tr.insert(s);
    }
    tr.query();
    for (int i = tr.ans.size()-1 ; i>=0 ; i--) {
        if (tr.ans[i] != '-')
            break;
        tr.ans.pop_back();
    }
    cout<<tr.ans.size()<<'\n';
    for (int i = 0 ; i <tr.ans.size()-1; i++) {
        cout<<tr.ans[i]<<'\n';
    }
    cout<<tr.ans.back();

}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // precompute();
    int t = 1;
    // cin>>t;
    for (int i = 1; i <= t; i++) {
        solve();
    }
}
#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...