답안 #878378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878378 2023-11-24T09:19:01 Z vjudge1 Type Printer (IOI08_printer) C++14
100 / 100
105 ms 52760 KB
#include <iostream>
using namespace std;

string output;
char val[500001];
int m, sz, cnt_print;
bool ed[500001], mk[500001];

struct node{
    int son[26], cnt;
}nd[500001];

inline void add(const string &s){
    int cur = 0;
    for (int i = 0; i < s.size(); i++){
        if (!nd[cur].son[s[i] - 'a']){
            nd[cur].son[s[i] - 'a'] = ++sz;
        }
        val[nd[cur].son[s[i] - 'a']] = s[i];
        cur = nd[cur].son[s[i] - 'a'];
    }
    ed[cur] = true;
}

inline void mark(const string &s){
    int cur = 0;
    for (int i = 0; i < s.size(); i++){
        mk[nd[cur].son[s[i] - 'a']] = true;
        cur = nd[cur].son[s[i] - 'a'];
    }
}

void dfs(const int &x){
    if (ed[x] && x != 0){
        cnt_print++;
        output += 'P';
    }
    if (cnt_print == m){
        cout << output.size() << "\n";
        for (int i = 0; i < output.size(); i++){
            cout << output[i] << "\n";
        }
        return;
    }
    int cur = 0;
    for (int i = 0; i < 26; i++){
        cur = nd[x].son[i];
        if (!mk[cur] && cur != 0){
            output += val[cur];
            dfs(cur);
            output += '-';
        }
    }
    for (int i = 0; i < 26; i++){
        cur = nd[x].son[i];
        if (mk[cur] && cur != 0){
            output += val[cur];
            dfs(cur);
            output += '-';
        }
    }
}

int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin >> m;
    string max_size;
    for (int i = 1; i <= m; i++){
        string s;
        cin >> s;
        add(s);
        if (max_size.size() < s.size()){
            max_size = s;
        }
    }
    mark(max_size);
    dfs(0);
    return 0;
}

Compilation message

printer.cpp: In function 'void add(const string&)':
printer.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'void mark(const string&)':
printer.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'void dfs(const int&)':
printer.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for (int i = 0; i < output.size(); i++){
      |                         ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2504 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3164 KB Output is correct
2 Correct 3 ms 3416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5212 KB Output is correct
2 Correct 12 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 9856 KB Output is correct
2 Correct 5 ms 4172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 20724 KB Output is correct
2 Correct 90 ms 44812 KB Output is correct
3 Correct 43 ms 24048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 16880 KB Output is correct
2 Correct 105 ms 52760 KB Output is correct
3 Correct 54 ms 27004 KB Output is correct
4 Correct 84 ms 49936 KB Output is correct