Submission #441367

# Submission time Handle Problem Language Result Execution time Memory
441367 2021-07-05T05:46:20 Z dxz05 Type Printer (IOI08_printer) C++14
0 / 100
563 ms 71656 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

#define SZ(x) (int)(x).size()

const int MAXN = 6e5 + 3e2;

string s[MAXN], str[MAXN];

int next_free = 2;
int trie[MAXN][50];
int term[MAXN];

void add(int ind, string &s){
    int v = 1;
    for (int i = 0; i < s.size(); i++){
        int x = s[i] - 'a';
        if (trie[v][x] == 0) trie[v][x] = next_free++;
        v = trie[v][x];
    }
    term[v] = ind;
}

int idx = 0;

void sort(int v){
    if (term[v]) s[++idx] = str[term[v]];

    for (int i = 0; i < 50; i++){
        if (trie[v][i] != 0){
            sort(trie[v][i]);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    int ind = 0;
    for (int i = 1; i <= n; i++){
        cin >> str[i];
        if (str[i].size() > str[ind].size()) ind = i;
    }

    string longest = str[ind];

    for (int i = 1; i <= n; i++){
        int j = 0;
        while (j < str[i].size() && str[i][j] == longest[j]) j++;
        if (j) str[i] = char('z' + j) + str[i];
        add(i, str[i]);
        //cout << j << ' ' << str[i] << endl;
    }

    sort(1);

    vector<char> ans;
    for (int i = 1; i <= n; i++){
        //cout << s[i] << ' ';
        int j = 0;
        if (s[i][0] > 'z') s[i].erase(0, 1);

        int m = s[i - 1].size(), k = s[i].size();

        if (m > k){
            int need = m - k;
            while (need--) ans.push_back('-');
            m = k;
        }

        int cnt = 0;
        for (int j = 0; j < min(m, k); j++){
            if (s[i - 1][j] == s[i][j]) cnt++; else
                break;
        }

        int need = m - cnt;
        while (need--){
            ans.push_back('-');
        }

        for (int j = cnt; j < k; j++){
            ans.push_back(s[i][j]);
        }
        ans.push_back('P');

    }

    cout << ans.size() << endl;
    for (char c : ans) cout << c << endl;

    return 0;
}

Compilation message

printer.cpp: In function 'void add(int, std::string&)':
printer.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         while (j < str[i].size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
printer.cpp:65:13: warning: unused variable 'j' [-Wunused-variable]
   65 |         int j = 0;
      |             ^
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 37836 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 37892 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 37848 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 37912 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 37940 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 45 ms 39312 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 107 ms 43072 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 240 ms 51136 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 563 ms 71656 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 472 ms 63980 KB printed invalid word
2 Halted 0 ms 0 KB -