Submission #889355

# Submission time Handle Problem Language Result Execution time Memory
889355 2023-12-19T11:53:24 Z dimashhh Type Printer (IOI08_printer) C++17
0 / 100
2 ms 600 KB
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 1,MOD = 1e9 + 9;
typedef long long ll;

int n;

struct trie{
    trie *nx[30];
    int sum = 0;
    int ok = 0;
    ll dp = 0,dp1 = 0;
    trie():sum(0),ok(0){
        dp1 = dp = 0;
        for(int i = 0;i < 30;i++){
            nx[i] = nullptr;
        }
    }
};
int q;
trie *root = new trie();
void add(string &s,trie *cur){
    cur->sum++;
    for(int i = 0;i < (int)s.size();i++){
        int nv = (s[i] - 'a');
        if(!cur->nx[nv]){
            cur->nx[nv] = new trie();
        }
        cur = cur->nx[nv];
        cur->sum++;
    }
    cur->ok++;
}
vector<char> res;
int sz(trie *cur){
    return (cur ? cur->dp1 : 0);
}
void calc(trie *cur){
    cur->dp = 2;
    int p[26];
    iota(p,p + 26,0);
    for(auto i:p)
    {
        if(cur->nx[i]){
            calc(cur->nx[i]);
            cur->dp += cur->nx[i]->dp;
            cur->dp1 = max(cur->dp1,cur->nx[i]->dp1 + 1);
        }
    }
}
void go(trie *cur){
    if(cur->ok){
        res.push_back('P');
    }
    int p[26];
    iota(p,p + 26,0);
    sort(p,p + 26,[&](int x,int y){
        return sz(cur->nx[x]) < sz(cur->nx[y]);
    });

    for(auto i:p)
    {
        if(cur->nx[i]){
            res.push_back((char)(i + 'a'));
            go(cur->nx[i]);
        }
    }
    res.push_back('-');
}
void test(){
    cin >> n;
    while(n--){
        string s;
        cin >> s;
        add(s,root);
    }
    calc(root);
    go(root);
    while(!res.empty() && res.back() != 'P') res.pop_back();
    cout << res.size() << '\n';
    for(auto i:res){
        cout << i << '\n';
    }
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    freopen("printer.in","r",stdin);
    freopen("printer.out","w",stdout);
    int T = 1;
    // cin >> T;
    for(int test_case = 1;test_case <= T;test_case++){
        test();
    }
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     freopen("printer.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("printer.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 600 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 344 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 600 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 344 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -