Submission #445725

# Submission time Handle Problem Language Result Execution time Memory
445725 2021-07-19T12:05:40 Z dxz05 Type Printer (IOI08_printer) C++14
80 / 100
1000 ms 130688 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

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

const int MAXN = 6e5 + 3e2;

string 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;
}

string s[MAXN];
int arr2[MAXN];
int arr[MAXN];

int idx = 0;

void sort(int v){
    if (term[v]) s[++idx] = str[term[v]], arr2[idx] = arr[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] = string(j, '{') + str[i];
        arr[i] = j;
        add(i, str[i]);
        //cout << j << ' ' << str[i] << endl;
    }

    sort(1);

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

        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;
}

/**
3
print
the
poem
*/

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:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         while (j < str[i].size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
printer.cpp:69:13: warning: unused variable 'j' [-Wunused-variable]
   69 |         int j = 0;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 22 ms 37836 KB Output is correct
2 Correct 22 ms 37884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 37916 KB Output is correct
2 Correct 22 ms 37932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 37848 KB Output is correct
2 Correct 27 ms 37852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 37964 KB Output is correct
2 Correct 23 ms 37980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 38056 KB Output is correct
2 Correct 34 ms 38604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 39340 KB Output is correct
2 Correct 51 ms 39716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 43172 KB Output is correct
2 Correct 229 ms 49348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 245 ms 51260 KB Output is correct
2 Correct 87 ms 41188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 576 ms 71772 KB Output is correct
2 Execution timed out 1079 ms 116168 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 470 ms 64120 KB Output is correct
2 Execution timed out 1080 ms 130688 KB Time limit exceeded
3 Halted 0 ms 0 KB -