답안 #333048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333048 2020-12-04T11:24:06 Z FutymyClone Type Printer (IOI08_printer) C++14
10 / 100
180 ms 90984 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 -> 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:51: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]
   51 |     for (int i = 0; i < tmp.size(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:73: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]
   73 |     for (int i = 0; i < vec.size(); i++) add(vec[i], i);
      |                     ~~^~~~~~~~~~~~
printer.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 0; i < ans.size(); i++) cout << ans[i] << "\n";
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 2028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 6512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 34 ms 16240 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 40328 KB Output is correct
2 Correct 180 ms 90984 KB Output is correct
3 Incorrect 114 ms 47972 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 72 ms 31848 KB Output isn't correct
2 Halted 0 ms 0 KB -