답안 #546867

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546867 2022-04-08T17:22:35 Z ussef_abdallah Type Printer (IOI08_printer) C++14
100 / 100
56 ms 5572 KB
#include <bits/stdc++.h>

using namespace std;

int common(string &x, string &y) {
    int mn = min(x.length(), y.length());
    int cnt = 0;
    for (int i = 0; i < mn; i++) {
        if (x[i] == y[i])
            cnt++;
        else
            break;
    }
    return cnt;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<string> a(n);
    string longest = "";
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i].length() > longest.length())
            longest = a[i];
    }
    sort(a.begin(), a.end(), [&](string &x, string &y) {
        int commonX = common(x, longest);
        int commonY = common(y, longest);
        if (commonX == commonY)
            return x < y;
        return commonX < commonY;
    });
    string cur = "";
    vector<char> ans;
    for (string &s : a) {
        int sPtr = 0, curPtr = 0;
        while (curPtr < cur.length() && sPtr < s.length()
               && cur[curPtr] == s[sPtr]) {

            curPtr++;
            sPtr++;
        }
        for (int j = cur.length() - 1; j >= curPtr; j--) {
            cur.pop_back();
            ans.push_back('-');
        }
        while (sPtr < s.length()) {
            ans.push_back(s[sPtr]);
            cur += s[sPtr];
            sPtr++;
        }
        ans.push_back('P');
    }
    cout << ans.size() << '\n';
    for (char &c : ans) {
        cout << c << "\n";
    }
    return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         while (curPtr < cur.length() && sPtr < s.length()
      |                ~~~~~~~^~~~~~~~~~~~~~
printer.cpp:40:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         while (curPtr < cur.length() && sPtr < s.length()
      |                                         ~~~~~^~~~~~~~~~~~
printer.cpp:50:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         while (sPtr < s.length()) {
      |                ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 260 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 3 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 596 KB Output is correct
2 Correct 9 ms 1056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1112 KB Output is correct
2 Correct 8 ms 1136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2120 KB Output is correct
2 Correct 49 ms 4712 KB Output is correct
3 Correct 30 ms 3704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 1996 KB Output is correct
2 Correct 56 ms 5572 KB Output is correct
3 Correct 37 ms 4132 KB Output is correct
4 Correct 48 ms 5412 KB Output is correct