제출 #1337325

#제출 시각아이디문제언어결과실행 시간메모리
1337325matsakyannnType Printer (IOI08_printer)C++20
0 / 100
165 ms327680 KiB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <cstdio>
//#include <sys/resource.h>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(deque <T> v);
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(queue <T> q) {cerr << "[ "; while(!q.empty()) {print(q.front()); cerr << " "; q.pop();} cerr << "]";}
template <class T> void print(priority_queue <T> q) {cerr << "[ "; while(!q.empty()) {print(q.top()); cerr << " "; q.pop();} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(int n__, T arr[]) {cerr << "[ "; for(int i = 0; i <= n__; i++) {print(arr[i]); cerr << " ";} cerr << "]\n";}
 
 
#define pb push_back
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define noAnsw cout << -1 << '\n'
#define ll long long
#define ppb pop_back
#define OK cout << "OK" << endl;
#define ld long double
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define PII pair <int, int>
#define endl '\n'
#define ull unsigned long long
#define PLL pair <ll, ll>
#define PIL pair <int, ll>
#define PLI pair <ll, int>
#define rall(v) (v).rbegin(), (v).rend() 
#define priora priority_queue
#define urishOK cout << "urishOK\n";
 
// void increaseStack(rlim_t stackSize) {
//     struct rlimit rl;
//     int result;
//     result = getrlimit(RLIMIT_STACK, &rl);
//     if (result == 0) {
//         if (rl.rlim_cur < stackSize) {
//             rl.rlim_cur = stackSize;
//             result = setrlimit(RLIMIT_STACK, &rl);
//             if (result != 0) {
//                 cerr << "Error setting stack size." << endl;
//             }
//         }
//     }
// }
void setIO(string name = "") {
	if (!name.empty()) {
		freopen((name + ".in").c_str(), "r", stdin);  
		freopen((name + ".out").c_str(), "w", stdout);
	}
}
void fastIO(){
    ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL); 
}
void setPrecision(int x){
    if(x == 0){
        return;
    }
    cout.setf(ios::fixed);
    cout.precision(x);
    return;
}
 
const int inf = 1e9 + 5, LOG = 30;
const ll inf64 = 1e18 + 5, mod = 1e9 + 7, wmod = 998244353;
const double epsilon = 0.000001, pi = 3.14159265359;
 
const int N = 25005;
int n, dp[25 * N][2], val[25 * N];
deque <int> dq[25 * N];
string s[N];
struct BOR{
    int tree[25 * N][26], timer = 1;

    void upd(int indx){
        int curr = 1;
        string str = s[indx];
        for(char c : str){
            int i = (c - 'a');
            if(!tree[curr][i]) tree[curr][i] = ++timer;
            curr = tree[curr][i];
        }
        val[curr] = indx;
    }

    void dfs(int node = 1){
        // dbg(node);
        // dbg(val[node]);
        for(int i = 0; i < 26; i++){
            if(!tree[node][i]) continue;
            dfs(tree[node][i]);
        }
        int children = 0;
        for(int i = 0; i < 26; i++){
            if(!tree[node][i]) continue;
            dp[node][0] += 2 + dp[tree[node][i]][0];
            children++;
        }
        int minimal_i;
        dp[node][1] = inf;
        for(int i = 0; i < 26; i++){
            if(!tree[node][i]) continue;
            int u = tree[node][i];
            if(dp[node][0] - 2 - dp[u][0] + dp[u][1] + 1 < dp[node][1]){
                dp[node][1] = dp[node][0] - 2 - dp[u][0] + dp[u][1] + 1;
                minimal_i = i;
            }
        }
        //dbg(MP(node, minimal_i));
        for(int i = 0; i < 26; i++){
            int u = tree[node][i];
            if(!u) continue;
            if(minimal_i == i){
                if(dq[node].size() < dq[u].size()){
                    dq[node].swap(dq[u]);
                    while(!dq[u].empty()){
                        dq[node].push_front(dq[u].back());
                        dq[u].pop_back();
                    }
                }
                else{
                    while(!dq[u].empty()){
                        dq[node].push_back(dq[u].front());
                        dq[u].pop_front();
                    }
                }
            }
            else{
                if(dq[node].size() < dq[u].size()){
                    dq[node].swap(dq[u]);
                    while(!dq[u].empty()){
                        dq[node].push_back(dq[u].front());
                        dq[u].pop_front();
                    }
                }
                else{
                    while(!dq[u].empty()){
                        dq[node].push_front(dq[u].back());
                        dq[u].pop_back();
                    }
                }
            }
            // dbg(node);
            // dbg(dq[node]);
        }
        if(dp[node][1] == inf) dp[node][1] = 0;
        if(val[node]){
            dp[node][0]++;
            dp[node][1]++;
            dq[node].push_front(val[node]);
        }
    }
} bor;

void solve(int TST_NUM){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> s[i];
        bor.upd(i);
    }
    bor.dfs();
    string curr = "";
    vector <char> answ;
    while(!dq[1].empty()){
        string target = s[dq[1].front()]; dq[1].pop_front();
        while(!curr.empty() && curr != target.substr(0, curr.size())){
            answ.pb('-');
            curr.ppb();
        }
        while(curr.size() < target.size()){
            answ.pb(target[curr.size()]);
            curr.pb(target[curr.size()]);
        }
        answ.pb('P');
    }
    cout << dp[1][1] << endl;
    for(char c : answ){
        cout << c << endl;
    }
    // dbg(dq[1]);
    // for(int i = 1; i <= 12; i++){
    //     for(int j = 0; j < 2; j++){
    //         cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
    //     }
    // }
}   
 
int main(){
    setPrecision(1);
    fastIO();
    //increaseStack(64 * 1024 * 1024);
    //setIO("file");
 
    int Tests = 1;
    //cin >> Tests;
 
    int TST_NUM = 1;
    while(Tests--){
        solve(TST_NUM++);
    }
    return 0; 
} 
//mcmcmcmcmcmcmcmcmcmcmc
//mcmcmcmcmcmcmcmcmcmcmc
//mcmcmcmcmcmcmcmcmcmcmc
//mcmcmcmcmcmcmcmcmcmcmc
//matsak

컴파일 시 표준 에러 (stderr) 메시지

printer.cpp: In function 'void setIO(std::string)':
printer.cpp:74:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |                 freopen((name + ".in").c_str(), "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:75:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |                 freopen((name + ".out").c_str(), "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...