Submission #1015930

# Submission time Handle Problem Language Result Execution time Memory
1015930 2024-07-07T05:45:57 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
98 ms 56488 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define _GLIBCXX_FILESYSTEM
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e5+2;
int sz[N];

struct Trie{
    struct node{
        int nxt[26];
        bool end;
        node(){
            memset(nxt,-1,sizeof(nxt));
            end = 0;
        }
    };
    vector<node> trie;
    Trie(){
        trie.emplace_back();
    }

    void insert(string &s){
        int cur = 0;
        for(int i = 0; i < s.size(); i++){
            if(trie[cur].nxt[s[i]-'a'] == -1){
                trie[cur].nxt[s[i]-'a'] = trie.size();
                trie.push_back(node());
            }
            cur = trie[cur].nxt[s[i]-'a'];
        }
        trie[cur].end = 1;
    }
};
Trie trie;

void dfs_sz(int cur){
    for(int i = 0; i < 26; i++){
        int v = trie.trie[cur].nxt[i];
        if(v != -1){
            dfs_sz(v);
            sz[cur] = max(sz[cur],sz[v]);
        }
    }
    sz[cur]++;
}
vector<char> ans;

void dfs(int cur,bool rakhbo = 1){
    bool skip = 0;
    int bigChild = -1;
    for(int i = 0; i < 26; i++){
        int v = trie.trie[cur].nxt[i];
        if(v == -1) continue;
        // cerr << cur << ' ' << v << ' ' << sz[v] << '\n';
        if(sz[v] == sz[cur]-1 and !skip){
            skip = 1;
            bigChild = i;
            continue;
        }
        ans.push_back((char)('a'+i));
        if(trie.trie[v].end) ans.push_back('P');
        dfs(v,0);
    }
    if(~bigChild){
        int v = trie.trie[cur].nxt[bigChild];
        ans.push_back((char)('a'+bigChild));
        if(trie.trie[v].end) ans.push_back('P');
        dfs(v,(1&rakhbo));
    }
    if(!rakhbo) ans.push_back('-');
}

void solve() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        string s;
        cin >> s;
        trie.insert(s);
    }
    dfs_sz(0);
    dfs(0);
    cout << ans.size() << '\n';
    for(auto x: ans) cout << x << '\n';
    return;
}

int32_t main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    // cin >> tc;
    for(int kase = 1; kase <= tc; kase++) {
        //cout << "Case " << kase << ": ";
        solve();
    }
    return 0;
}

Compilation message

printer.cpp: In member function 'void Trie::insert(std::string&)':
printer.cpp:26:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |         for(int i = 0; i < s.size(); i++){
      |                        ~~^~~~~~~~~~
# 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 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 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 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1320 KB Output is correct
2 Correct 3 ms 2160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5388 KB Output is correct
2 Correct 15 ms 8012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 7880 KB Output is correct
2 Correct 6 ms 2420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 30148 KB Output is correct
2 Correct 84 ms 56284 KB Output is correct
3 Correct 35 ms 29892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 15284 KB Output is correct
2 Correct 98 ms 56380 KB Output is correct
3 Correct 55 ms 30144 KB Output is correct
4 Correct 80 ms 56488 KB Output is correct