Submission #838787

# Submission time Handle Problem Language Result Execution time Memory
838787 2023-08-27T18:23:10 Z kingmessi Type Printer (IOI08_printer) C++17
100 / 100
964 ms 135424 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define repin rep(i,0,n)
#define di(a) int a;cin>>a;
#define precise(i) cout<<fixed<<setprecision(i)
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define take(a,n) for(int j=0;j<n;j++) cin>>a[j];
#define give(a,n) for(int j=0;j<n;j++) cout<<a[j]<<' ';
#define vpii vector<pair<int,int>>
#define sis string s;
#define sin string s;cin>>s;
#define db double
#define be(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bpc(x) __builtin_popcountll(x) 
#define btz(x) __builtin_ctz(x)
using namespace std;

using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<pair<int, int>, null_type,less<pair<int, int> >, rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;

const long long INF=1e18;
const long long M=1e9+7;
const long long MM=998244353;
  
int power( int N, int M){
    int power = N, sum = 1;
    if(N == 0) sum = 0;
    while(M > 0){if((M & 1) == 1){sum *= power;}
    power = power * power;M = M >> 1;}
    return sum;
}

const int SZ= 5000005;
int nex[SZ][26], en[SZ], num = 0;
vi h(SZ);
int n;
int cnt = 0;
vector<char> ans;
void ins(string &s) {
    int cur = 0;
    int n = s.length();
    repin {
        if (!nex[cur][s[i]-'a']) nex[cur][s[i]-'a'] = ++num;
        cur = nex[cur][s[i]-'a'];
        if(i==n-1)en[cur]++;
    }
}

void get(int cur){
    rep(i,0,26){
        if(nex[cur][i]){
            get(nex[cur][i]);
            h[cur]=max(h[cur],h[nex[cur][i]]+1);
        }
    }
}

void dfs(int cur){
    if(en[cur]){ans.pb('P');cnt++;}
    vpii v;
    rep(i,0,26){
        if(nex[cur][i]){
            v.pb({h[nex[cur][i]],i});
        }
    }
    sort(be(v));
    for(auto x : v){
        int i = x.ss;
        char c = 'a'+i;
        ans.pb(c);
        dfs(nex[cur][i]);
        if(cnt!=n)ans.pb('-');
    }
}

 
void solve()
{
    cin>>n;
    repin{
        string s;
        cin>>s;
        ins(s);
    }
    get(0);
    dfs(0);
    cout<<ans.size()<<endl;
    for(auto x : ans)cout<<x<<endl;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifdef NCR
        init();
    #endif
    #ifdef SIEVE
        sieve();
    #endif
        solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 39380 KB Output is correct
2 Correct 14 ms 39404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 39380 KB Output is correct
2 Correct 14 ms 39380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 39508 KB Output is correct
2 Correct 14 ms 39508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 39508 KB Output is correct
2 Correct 14 ms 39380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 39508 KB Output is correct
2 Correct 22 ms 40192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 40916 KB Output is correct
2 Correct 35 ms 41356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 44948 KB Output is correct
2 Correct 136 ms 51276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 53452 KB Output is correct
2 Correct 57 ms 42452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 368 ms 74576 KB Output is correct
2 Correct 842 ms 120080 KB Output is correct
3 Correct 441 ms 80924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 66928 KB Output is correct
2 Correct 964 ms 135424 KB Output is correct
3 Correct 501 ms 86492 KB Output is correct
4 Correct 899 ms 129884 KB Output is correct