Submission #440535

# Submission time Handle Problem Language Result Execution time Memory
440535 2021-07-02T11:40:32 Z dxz05 Type Printer (IOI08_printer) C++14
80 / 100
212 ms 28928 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

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

const int MAXN = 1e5 + 3e2;

string str[MAXN];

int next_free = 2;
int trie[MAXN][27];
bool term[MAXN];

void add(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] = true;
}

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

    for (int i = 0; i <= 26; i++){
        if (trie[v][i] != 0) sort(trie[v][i], s + char('a' + 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() && j < longest.size() && str[i][j] == longest[j]) j++;
        if (j) str[i] = string(j, '{') + str[i];
        add(str[i]);
    }

    sort(1, "");

    vector<char> ans;
    for (int it = 1; it <= n; it++){
        int j = 0;
        while (j < str[it].size() && str[it][j] == '{') j++;
        str[it].erase(0, j);

        string s = str[it], t = str[it - 1];

        if (SZ(t) > SZ(s)){
            int need = SZ(t) - SZ(s);
            while (need--) ans.push_back('-');
            t.resize(s.size());
        }

        int cnt = 0;
        for (int i = 0; i < min(SZ(t), SZ(s)); i++){
            if (t[i] == s[i]) cnt++; else
                break;
        }

        int need = max(0, SZ(t) - cnt);
        while (need--){
            ans.push_back('-');
        }

        for (int i = cnt; i < s.size(); i++){
            ans.push_back(s[i]);
        }
        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(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:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         while (j < str[i].size() && j < longest.size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
printer.cpp:51:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         while (j < str[i].size() && j < longest.size() && str[i][j] == longest[j]) j++;
      |                                     ~~^~~~~~~~~~~~~~~~
printer.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         while (j < str[it].size() && str[it][j] == '{') j++;
      |                ~~^~~~~~~~~~~~~~~~
printer.cpp:83:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int i = cnt; i < s.size(); i++){
      |                           ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3404 KB Output is correct
2 Correct 15 ms 3884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4244 KB Output is correct
2 Correct 31 ms 4484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 6340 KB Output is correct
2 Correct 187 ms 9712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 10816 KB Output is correct
2 Correct 64 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 28928 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 28404 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -