Submission #1313137

#TimeUsernameProblemLanguageResultExecution timeMemory
1313137raymoo_Type Printer (IOI08_printer)C++17
100 / 100
170 ms119584 KiB
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int, int> #define bend(v) v.begin(),v.end() #define vect vector #define prq priority_queue #define umap unordered_map #define eb emplace_back #define pb push_back #define pob pop_back #define ef emplace_front #define pf push_front #define pof pop_front #define el "\n" #define deb cout<<"\nok\n";return #define nextl cout<<"\n" #define lwb lower_bound #define upb upper_bound #define rs resize #define popcnt __builtin_popcount #define clz __builtin_clz #define ctz __builtin_ctz #define ll long long #define dbl long double #define int long long #define FILE "ijustwannabepartofyourskibidi" void IO(){ if(fopen(FILE".in", "r")){ freopen(FILE".in", "r", stdin); freopen(FILE".out", "w", stdout); } else if(fopen(FILE".inp", "r")){ freopen(FILE".inp", "r", stdin); freopen(FILE".out", "w", stdout); } } const ll N = 3e5 + 1, MOD = 1e9+7, INF = 1000000000000000069; mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r){ return uniform_int_distribution<ll>(l, r)(rng); } ll pm(ll a,const ll b=MOD){return ((a%b)+b)%b;} ll sq(ll x){return x*x;} ll __lcm(ll a, ll b, const ll lim=LLONG_MAX){ if(a == -1 || b == -1)return -1; ll g = __gcd(a,b); if(b/g > lim/a)return -1; return (a/g)*b; } struct node{ int nxt[26], priority, end_here; char val; node(char v = 0){ val = v; priority = -1; end_here = 0; memset(nxt, -1, sizeof nxt); } }; pii dp[1000069][2]; // dp[u][0]: subtree u, visit all nodes and stop at a leaf // dp[u][1]: subtree u, visit all nodes and return to u struct Trie{ vect<node> t; Trie(){ t.pb(node()); } void add(const string &s){ int p = 0; for(char c : s){ if(t[p].nxt[c - 'a'] != -1) p = t[p].nxt[c - 'a']; else{ t[p].nxt[c - 'a'] = t.size(); p = t.size(); t.pb(node(c)); } } t[p].end_here++; } void dfs(int u){ pii mn = {INF, 0}; for(int i=0; 26>i; i++) if(t[u].nxt[i] != -1){ int v = t[u].nxt[i]; dfs(v); dp[u][1].fi += dp[v][1].fi + 2; dp[u][0].fi += dp[v][1].fi + 2; mn = min(mn, make_pair(dp[v][0].fi - dp[v][1].fi - 1, i)); } if(mn.fi < INF){ dp[u][0].fi += mn.fi, dp[u][0].se = mn.se; t[u].priority = mn.se; } } void dfs2(int u, int should_del){ if(u) cout << t[u].val << el; for(int i=0;t[u].end_here > i; i++) cout << "P\n"; if(should_del){ // not the most optimal path for(int i=0; 26>i; i++){ if(t[u].nxt[i] != -1) dfs2(t[u].nxt[i], should_del); } cout << "-\n"; }else{ for(int i=0; 26>i; i++) if(t[u].nxt[i] != -1 && i != t[u].priority){ dfs2(t[u].nxt[i], 1); } if(t[u].priority != -1) dfs2(t[u].nxt[t[u].priority], 0); } } }; void sol(){ int n; cin >> n; Trie trie; for(int i=0; n>i; i++){ string s; cin >> s; trie.add(s); } trie.dfs(0); cout << dp[0][0].fi + n << el; trie.dfs2(0, 0); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); IO(); int t = 1; // cin >> t; while(t--) sol(); } /* ...-%%%%%%%%%%%... .:**#%=%%%%%+........-%%%%. . .%%%%%%=-..%#. .#%%. %%%+.. .: *%%. .%%. :. :%%%%%:. .%%. ... .: :: .=%%%. ..%%. #%#:%. : :.. =%%. %%+..: .%*::::%-::::::-::::::.. .:-%%%%. .*%%.. .%# :.%:::::::%%%%%%%%%%%%%+...%%%+::::%#. .%%. .%%. ..:.%=::::*%#***#%::::::::%%%%=:::::::%%. .%%. =%%%...-%%%:::*%********%:::%%%****%::::::::%% .%- .%::::%%***%=:%%**********%%********%:::::::=%-:. .%.. #%:::%#*****%%**********************%:::+%%%*%%-.: .::..%%. .%%::%%*****==**==*****#************#%%%%#******%%..::::.. .%%. .%%#%+%%***************##*+=+*==********************%%.:. .%% .%%***%%****************#**==**==******==**+=+********%%%%%%%%% %%. .#%#*********************%%**************++************%%::::::%* .%%%. .%%***************#*****%%%****************************%%::::::%% .-%%%. .#%***************#****%%--%*********%*****************%#::::-%%%%%%%%%%. .%%**************#**%%%----%********%%**************#***#%%%#**%#******%* .%%**************#%%%--.....%%*******%%**************#**********#%******%%. .%%*************%%...........%%******%-%*************#***********%%*****%%. .%%*************%%...%%%%%.....%%%%%%%.=%%%%%%%%##****#****##::::+%%*****%# .%%************%%...%. %%.................%%%%-.....%*****#*****%%*****%% .#%#*#****#****%%:::%- %%.................%. #-....%*****#*#####%#****%%. .%%##****%%%**%%:::::##...................%= .%+:::%#*****#:####*%%****%%. .+%%#**%%.#%%%%::::::........###+.:*##-....%%%:::::%*****##******%%****%%- ..%%*%%.......:::.......##..#*...#+..#*....:::::%******#*****##%%*****%%. .%%%%%................#-##=--##+-+###-#....:::%******#:###*::%%#*****%%. .%%****%%..............#----------------=*...#%#****##**##::#**%%*******%%. .%%%%%*=+%%*...........#-----------------#.....#%%%%%%+:##*##:*%%*******%%%. ..:*%====%%%%........#-----------------#:.........%#********%%*****%%%%%%. .%%=======*%%%%%%%.%%+----#-%------%%#.......%%%%::::::::%%==***#%###%%. ..%%=============+%%-:#%%%%-::%%%%%%::%%%%%%%%%=%%*******%%=====**%%###%%.. -%%%%%%%%%%%%%%%%+%.%+:+%::%::::%::%:::%%#%... .%%******%%%========#%####%%. .%%++#############%%# ..%%.**%%#%%%%%%%#%***%. :%%*****%%%=+%%%%%%%%%%#####%%. .%%++++*############*++% .%************#****%. #%*%%%%+++%%%%###############%%. .+%%%%%%%%%%####+##########*+++++%%. .%**#**********#****% %%**%%%++++###################%: =%%%%######################++++++++%%*%.-%%#***********#****% .%****%%+++++###################%% .%%%#######################*%%%%%%%%.%%*#%+%###################%.%%***%%%+++++###################%% *%%######################+#%%++++++%*. =%*%. .:%%%%########%%%%%%%%%#*#%.%%++#####################%% .%%%#####################%%%%%+++++++%- %*#%%. .%%%*++++++*%%%....%%#####################%%* %%######%%%%%#%%%%%######%%.%*+++++++%. %****#. .%%+++*+++++*++%%...=%%###################%%%. %%%%%%%%#-=##%...%%######%%.%%+++++++%%..*%***#%%%%%. ..%+++++++++++++++%%...%%##################%%%. .%:. .#%#####%%.%%+++++++++%. %*********%%%%%++++++++++++++++%%...%%##############%%%%.. :%#####%% .%%+++++++%% .%%**********%%++++++++++++++++%%.....%#########%%%%%% . *%#####%%. .%%+++++++%%:. .%%********%%++++++++++++++++%%....%%%%%%%%%%%%*.. .%%##%%% .%%*+++++++%%. ..#%%%#****%%+++++++++++++++%%%%%% . ..... %%#%%%. .:%%%%%%%%%%%...-%. ......%%+++++++++++++%%. ..%%%. ... .%%%#%%%=:#%%%%%%%%*++++++%%%# .-%%+. ... .%%%%%%%:. */

Compilation message (stderr)

printer.cpp: In function 'void IO()':
printer.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen(FILE".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen(FILE".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(FILE".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(FILE".out", "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...