-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006-ioi-08-p1-type-printer.cpp
More file actions
117 lines (116 loc) · 2.5 KB
/
Copy path006-ioi-08-p1-type-printer.cpp
File metadata and controls
117 lines (116 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* IOI '08 P1 - Type Printer [ioi08p1]
* Problem: https://dmoj.ca/problem/ioi08p1
* Verdict: ACCEPTED (12.0 pts) Solved: 2022-12-12
* Language: C++20
* Runtime: 0.445173839 s Memory: 51176.0 KB
* Source: https://dmoj.ca/src/5115345
*/
/*
ID: BidoTeima
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void moo(string filename);
void ACPLS(string str = "")
{
if(str=="NOF")return;
if(str.size())
moo(str);
else{
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
}
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void moo(string fileName){
freopen((fileName+".in").c_str(),"r",stdin);
freopen((fileName+".out").c_str(),"w",stdout);
}
#define tc \
int tttttt/*,subtask*/; \
cin >> tttttt/* >> subtask*/; \
while (tttttt--)/*end
*/
int children[500010][26];
int dp[500010];
bool End[500010];
int dummyValue;
void insert(const string& s){
int c = 1;
for(char ch : s){
int x = ch - 'a';
if(!children[c][x]){
children[c][x]=dummyValue;
dummyValue++;
}
c = children[c][x];
}
End[c] = 1;
}
int depth(int n){
if(dp[n]!=-1)
return dp[n];
int mx = 0;
for(int i = 0; i < 26; i++){
int c = children[n][i];
if(c){
mx=max(mx,depth(c));
}
}
return dp[n] = mx + 1;
}
vector<char>output;
void dfs(int n){
if(End[n])output.push_back('P');
int childWithMaxDep=-1,mxDep=0;
for(int i = 0; i < 26; i++){
int c = children[n][i];
if(c){
int dist=depth(c);
if(mxDep<dist){
mxDep=dist;
childWithMaxDep=i;
}
}
}
for(int i = 0; i < 26; i++){
if(i == childWithMaxDep)continue;
int c = children[n][i];
if(c){
output.push_back(i+'a');
dfs(c);
output.push_back('-');
}
}
if(childWithMaxDep!=-1){
output.push_back(childWithMaxDep+'a');
dfs(children[n][childWithMaxDep]);
output.push_back('-');
}
}
int main()
{
memset(dp,-1,sizeof(dp));
ACPLS();
dummyValue = 2;
int n;
cin>>n;
for(int i = 0; i < n; i++){
string s;
cin>>s;
insert(s);
}
depth(1);
dfs(1);
while(output.back()=='-')output.pop_back();
cout<<output.size()<<'\n';
for(char ch : output)cout<<ch<<'\n';
}