답안 #555461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555461 2022-05-01T01:42:29 Z Sharky Type Printer (IOI08_printer) C++17
100 / 100
115 ms 94396 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
using vi = vector<int>;
using vb = vector<bool>;
using pi = pair<int, int>;

#define pb push_back
#define all(x) x.begin(), x.end()
#define FOR(i, a, b) for (int i = (int) a; i < (int) b; i++)
#define rep(i, n) for (int i = 0; i < (int) n; i++)
#define rep1(i, n) for (int i = 1; i <= (int) n; i++)
#define sz(x) (int) x.size()
#define fi first
#define se second
#define rd(a, sz) rep(i, n) cin >> a[i];
#define rd1(a, sz) rep1(i, n) cin >> a[i];

void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << '}';}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define debug(...)
#endif

const int MOD = 1e9 + 7;
const int inf = 1e18;
const int maxn = 25000 * 20 + 5;
int cnt = 0, nex[maxn][26], n;
vector<bool> flag(maxn, false), bac(maxn, false);
vector<char> ans;
void ins(string& s, bool f) {
    int p = 0;
    for (int i = 0; i < s.length(); i++) {
        int c = s[i] - 'a';
        if (!nex[p][c]) {
            nex[p][c] = ++cnt;
        }
        p = nex[p][c];
        flag[p] = f; 
        if (i == s.length() - 1) bac[p] = true; 
    }
}
void dfs(int u) {
    if (bac[u]) ans.pb('P');
    if (flag[u]) {
        for (int i = 0; i < 26; i++) {
            if (!flag[nex[u][i]] && nex[u][i]) {
                ans.pb((char) (i + 'a'));
                dfs(nex[u][i]);
                ans.pb('-');
            }
        }
        for (int i = 0; i < 26; i++) {
            if (flag[nex[u][i]] && nex[u][i]) {
                ans.pb((char) (i + 'a'));
                dfs(nex[u][i]);
                ans.pb('-');
            }
        }
    } else {
        for (int i = 0; i < 26; i++) {
            if (nex[u][i]) {
                ans.pb((char) (i + 'a'));
                dfs(nex[u][i]);
                ans.pb('-');
            }
        }
    }
}
void solve(int tc) {
    cin >> n;
    string mx = "";
    vector<string> vs; 
    rep(i, n) {
        string s; cin >> s; vs.pb(s);
        if (s.length() > mx.length() || (s.length() == mx.length() && s > mx)) mx = s;
    }
    for (auto& s : vs) {
        if (s != mx) ins(s, false);
    }
    ins(mx, true); 
    flag[0] = true; 
    dfs(0);
    while (ans.back() == '-') ans.pop_back();
    cout << sz(ans) << "\n";
    for (char c : ans) cout << c << "\n";
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int tt = 1; 
    // cin >> tt;
    rep1(i, tt) solve(i);
    return 0;
}

Compilation message

printer.cpp: In function 'void ins(std::string&, bool)':
printer.cpp:49:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for (int i = 0; i < s.length(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp:56:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         if (i == s.length() - 1) bac[p] = true;
      |             ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 448 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 3 ms 2260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5844 KB Output is correct
2 Correct 17 ms 12132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 14284 KB Output is correct
2 Correct 9 ms 3780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 35408 KB Output is correct
2 Correct 97 ms 79444 KB Output is correct
3 Correct 56 ms 42184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 27968 KB Output is correct
2 Correct 115 ms 94396 KB Output is correct
3 Correct 62 ms 47804 KB Output is correct
4 Correct 106 ms 89280 KB Output is correct