Submission #445743

# Submission time Handle Problem Language Result Execution time Memory
445743 2021-07-19T12:48:05 Z dxz05 Type Printer (IOI08_printer) C++14
80 / 100
1000 ms 70824 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][26], term[MAXN];

int mx[MAXN];

void add(int ind, int val, 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];
        mx[v] = max(mx[v], val);
    }
    term[v] = 1;
}

vector<char> ans;
int n, cnt = 0;

void solve(int v){
    if (term[v]){
        cnt++;
        ans.push_back('P');
        if (cnt == n){
            cout << ans.size() << endl;
            for (char c : ans) cout << c << endl;
            exit(0);
        }
    }

    int best = 0, ind = -1;

    for (int i = 0; i < 26; i++){
        if (trie[v][i] != 0){
            if (mx[trie[v][i]] > best){
                best = mx[trie[v][i]];
                ind = i;
            }
        }
    }

    for (int i = 0; i < 26; i++){
        if (trie[v][i] != 0 && ind != i){
            ans.push_back('a' + i);
            solve(trie[v][i]);
            ans.push_back('-');
        }
    }

    if (ind != -1){
        ans.push_back('a' + ind);
        solve(trie[v][ind]);
        ans.push_back('-');
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    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++;
        add(i, j, str[i]);
        //cout << j << ' ' << str[i] << endl;
    }

    solve(1);

    return 0;
}

/**
3
print
the
poem
*/

Compilation message

printer.cpp: In function 'void add(int, int, std::string&)':
printer.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:85:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         while (j < str[i].size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19148 KB Output is correct
2 Correct 12 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19120 KB Output is correct
2 Correct 11 ms 19148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19148 KB Output is correct
2 Correct 13 ms 19148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19116 KB Output is correct
2 Correct 11 ms 19112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19148 KB Output is correct
2 Correct 24 ms 19708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 19940 KB Output is correct
2 Correct 40 ms 20148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 22196 KB Output is correct
2 Correct 196 ms 25696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 26700 KB Output is correct
2 Correct 85 ms 20940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 553 ms 38216 KB Output is correct
2 Execution timed out 1087 ms 62776 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 479 ms 33816 KB Output is correct
2 Execution timed out 1088 ms 70824 KB Time limit exceeded
3 Halted 0 ms 0 KB -