답안 #445726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445726 2021-07-19T12:08:41 Z dxz05 Type Printer (IOI08_printer) C++14
80 / 100
1000 ms 130428 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][50];
int term[MAXN];

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

string s[MAXN];
int arr2[MAXN];
int arr[MAXN];

int idx = 0;

void sort(int v){
    if (term[v]) s[++idx] = str[term[v]], arr2[idx] = arr[term[v]];

    for (int i = 0; i < 50; i++){
        if (trie[v][i] != 0){
            sort(trie[v][i]);
        }
    }
}

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

    int n;
    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++;
        if (j) str[i] = string(j, '{') + str[i];
        arr[i] = j;
        add(i, str[i]);
        //cout << j << ' ' << str[i] << endl;
    }

    sort(1);

    vector<char> ans;
    for (int i = 1; i <= n; i++){
        int j = 0;
        if (arr2[i]) s[i].erase(0, arr2[i]);

        int m = s[i - 1].size(), k = s[i].size();

        if (m > k){
            int need = m - k;
            while (need--) ans.push_back('-');
            m = k;
        }

        int cnt = 0;
        for (int j = 0; j < min(m, k); j++){
            if (s[i - 1][j] == s[i][j]) cnt++; else
                break;
        }

        int need = m - cnt;
        while (need--){
            ans.push_back('-');
        }

        for (int j = cnt; j < k; j++){
            ans.push_back(s[i][j]);
        }
        ans.push_back('P');

    }

    cout << ans.size() << endl;
    for (char c : ans) cout << c << endl;

    return 0;
}

/**
3
print
the
poem
*/

Compilation message

printer.cpp: In function 'void add(int, std::string&)':
printer.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         while (j < str[i].size() && str[i][j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~
printer.cpp:70:13: warning: unused variable 'j' [-Wunused-variable]
   70 |         int j = 0;
      |             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 37836 KB Output is correct
2 Correct 22 ms 37836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 37948 KB Output is correct
2 Correct 23 ms 37908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 37836 KB Output is correct
2 Correct 21 ms 37964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 37964 KB Output is correct
2 Correct 21 ms 37964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 38056 KB Output is correct
2 Correct 37 ms 38696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 39332 KB Output is correct
2 Correct 54 ms 39852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 43120 KB Output is correct
2 Correct 205 ms 49348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 243 ms 51136 KB Output is correct
2 Correct 84 ms 41036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 568 ms 71544 KB Output is correct
2 Execution timed out 1095 ms 115664 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 484 ms 63768 KB Output is correct
2 Execution timed out 1100 ms 130428 KB Time limit exceeded
3 Halted 0 ms 0 KB -