Submission #445744

# Submission time Handle Problem Language Result Execution time Memory
445744 2021-07-19T12:57:53 Z dxz05 Type Printer (IOI08_printer) C++14
80 / 100
1000 ms 70488 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);

    scanf("%d", &n);

    int ind = 0;
    for (int i = 1; i <= n; i++){
        char c[25];
        scanf("%s", &c);
        int len = strlen(c);
        str[i].resize(len);
        for (int j = 0; j < len; j++) str[i][j] = c[j];
        //cout << str[i] << endl;
        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:78:17: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[25]' [-Wformat=]
   78 |         scanf("%s", &c);
      |                ~^   ~~
      |                 |   |
      |                 |   char (*)[25]
      |                 char*
printer.cpp:90:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         while (j < str[i].size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
printer.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         scanf("%s", &c);
      |         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19104 KB Output is correct
2 Correct 10 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19020 KB Output is correct
2 Correct 10 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19020 KB Output is correct
2 Correct 12 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19020 KB Output is correct
2 Correct 11 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19148 KB Output is correct
2 Correct 23 ms 19548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 19892 KB Output is correct
2 Correct 40 ms 20044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 21980 KB Output is correct
2 Correct 195 ms 25500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 234 ms 26584 KB Output is correct
2 Correct 74 ms 20772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 562 ms 37912 KB Output is correct
2 Execution timed out 1094 ms 62560 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 452 ms 33596 KB Output is correct
2 Execution timed out 1098 ms 70488 KB Time limit exceeded
3 Halted 0 ms 0 KB -