답안 #1038288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038288 2024-07-29T15:41:59 Z khanhtb Type Printer (IOI08_printer) C++14
0 / 100
90 ms 214992 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define sz(a) (ll)a.size()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll md = 998244353;
const ll smod = 1e6;
const ll inf = 1e18;
const ll imx = 1e9;
const ll blocksz = 320;
const ll N = 1e6+5; 
const ll lim = 1e6+5;
// Road to Expert: 136pts left
// Target: Top 1 CHD
// Ultimate Target: VOI 202k (5 <= k <= 6)
ll n;
struct node{
    ll exist,child[26];
    node(){
        exist = 0;
        for(int i = 0; i < 26; ++i) child[i] = 0;
    }
}; 
node trie[N];
ll now = 0;
void add(string &s){
    ll u = 0;
    for(auto v:s){
        ll k = v-'a';
        if(!trie[u].child[k]){
            trie[u].child[k] = ++now;
            trie[now] = node();
        }
        u = trie[u].child[k];
    }
    ++trie[u].exist;
}
ll h[N];
bool ok[N];
void dfs(ll u){
    ll gt = 0, cur = -1;
    h[u] = 1;
    for(int i = 0; i < 26; ++i){
        if(trie[u].child[i]){
            dfs(trie[u].child[i]);
            if(h[trie[u].child[i]] >= gt){
                gt = h[trie[u].child[i]];
                if(cur != -1) ok[cur] = 0;
                cur = trie[u].child[i];
                ok[cur] = 1;
            }
        }
    }
    h[u] = gt+1;
}
vector<char> res;
void get(ll u){
    for(int i = 0; i < trie[u].exist; ++i) res.pb('P');
    ll cur = -1;
    for(int i = 0; i < 26; ++i){
        if(trie[u].child[i]){
            if(ok[trie[u].child[i]]){cur = i; continue;}
            res.pb((char)(i+'a'));
            get(trie[u].child[i]);
            res.pb('-');
        }
    }
    if(cur != -1){
        res.pb((char)(cur+'a'));
        get(trie[u].child[cur]);
        res.pb('-');
    }
}
void solve(){
    cin >> n;
    trie[0] = node();
    for(int i = 1; i <= n; ++i){
        string s;cin >> s;
        add(s);
    }
    dfs(0);
    get(0);
    while(res.back() == '-') res.pop_back();
    cout << res.size() << '\n';
    for(auto v:res) cout << v << '\n';
}
int main(void){
    if(fopen("test.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    IOS
    int T = 1;
    // cin >> T;
    for(int t = 1; t <= T; ++t){
        // cout << "Case " << t << ":\n";
        solve();
        cout << '\n';
    }
    cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 60 ms 212052 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 57 ms 212052 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 58 ms 212052 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 65 ms 211984 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 58 ms 212068 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 60 ms 212312 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 64 ms 212564 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 77 ms 213340 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 87 ms 214992 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 90 ms 214480 KB Expected EOF
2 Halted 0 ms 0 KB -