Submission #333050

# Submission time Handle Problem Language Result Execution time Memory
333050 2020-12-04T11:28:11 Z FutymyClone Type Printer (IOI08_printer) C++14
100 / 100
216 ms 108128 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 5;

struct Trie {
    Trie *node[26];
    int num, priority;
    bool isEnd;

    Trie() {
        for (int i = 0; i < 26; i++) node[i] = NULL;
        num = 0; priority = 100000; isEnd = false;
    }
} *root = new Trie();

int n, nNode = 0;
vector <char> ans;
vector <string> vec;

void add (string s, int id) {
    Trie *cur = root;
    for (int i = 0; i < s.length(); i++) {
        if (cur -> node[s[i] - 'a'] == NULL) {
            cur -> node[s[i] - 'a'] = new Trie();
            cur = cur -> node[s[i] - 'a'];
            cur -> num = ++nNode;
            cur -> priority = id;
        } else {
            cur = cur -> node[s[i] - 'a'];
            cur -> priority = max(cur -> priority, id);
        }
    }

    cur -> isEnd = true;
}

void dfs (Trie *cur, char c, int p) {
    if (c != '#') ans.push_back(c);
    if (cur -> isEnd) ans.push_back('P');

    vector <pair <int, int> > tmp;
    for (int i = 0; i < 26; i++) {
        if (cur -> node[i] == NULL) continue;
        if (cur -> node[i] -> num == p) continue;
        tmp.push_back(make_pair(cur -> node[i] -> priority, i));
        //dfs(cur -> node[i], (char)(i + 'a'), cur -> num);
    }

    sort(tmp.begin(), tmp.end());
    for (int i = 0; i < tmp.size(); i++) {
        dfs(cur -> node[tmp[i].second], (char)(tmp[i].second + 'a'), cur -> num);
    }

    ans.push_back('-');
}

bool cmp (string s, string t) {
    if (s.length() != t.length()) return s.length() < t.length();
    return s < t;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        vec.push_back(s);
    }

    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < vec.size(); i++) add(vec[i], i);

    dfs(root, '#', 0);
    while (ans.size() > 0 && ans[ans.size() - 1] == '-') ans.pop_back();

    cout << (int)ans.size() << "\n";
    for (int i = 0; i < ans.size(); i++) cout << ans[i] << "\n";
    return 0;
}

Compilation message

printer.cpp: In function 'void add(std::string, int)':
printer.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i = 0; i < s.length(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp: In function 'void dfs(Trie*, char, int)':
printer.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 0; i < tmp.size(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for (int i = 0; i < vec.size(); i++) add(vec[i], i);
      |                     ~~^~~~~~~~~~~~
printer.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for (int i = 0; i < ans.size(); i++) cout << ans[i] << "\n";
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 2 ms 1260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2028 KB Output is correct
2 Correct 6 ms 2540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 6508 KB Output is correct
2 Correct 30 ms 13712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 16112 KB Output is correct
2 Correct 21 ms 4152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 40072 KB Output is correct
2 Correct 182 ms 90628 KB Output is correct
3 Correct 115 ms 47588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 31736 KB Output is correct
2 Correct 216 ms 108128 KB Output is correct
3 Correct 129 ms 54372 KB Output is correct
4 Correct 201 ms 102112 KB Output is correct