#include <bits/stdc++.h>
using namespace std;
#define MAXN 25005
vector<pair<int, char>> adj[20*MAXN];
int dp1[MAXN*20], dp2[MAXN*20];
int ans[MAXN*20];
int n;
int cur = 0;
pair<int, char> nodes[MAXN*20];
string ts;
int cnt = 0;
void addWord(int v, int p, int index){
if(index == ts.length()){
adj[v].push_back({++cur, 'P'});
adj[cur].push_back(nodes[v]);
nodes[cur] = {cur, 'P'};
return;
}
bool flag = false;
for(auto u : adj[v]){
if(p == u.first) continue;
if(u.second == ts[index]){
flag = true;
addWord(u.first, v, index+1);
}
}
if(!flag){
adj[v].push_back({++cur, ts[index]});
adj[cur].push_back(nodes[v]);
nodes[cur] = {cur, ts[index]};
addWord(cur, v, index+1);
}
}
void calc(int v, int p){
dp1[v] = dp2[v] = 0;
ans[v] = v;
int mi = 1000000000, mindex = -1;
for(auto u : adj[v]){
if(u.first == p){
continue;
}
calc(u.first, v);
dp1[v] += dp1[u.first]+2;
if(dp2[u.first]-dp1[u.first] < mi){
mi = dp2[u.first]-dp1[u.first];
mindex = u.first;
}
}
dp2[v] = dp1[v];
if(mi == 1000000000) return;
ans[v] = mindex;
dp2[v] = dp2[v]+mi-1;
}
void getAns(int v, int p){
char c;
for(auto u : adj[v]){
if(u.first == p) continue;
if(u.first == ans[v]){
c = u.second;
continue;
}
cout << u.second << endl;
getAns(u.first, v);
if(cnt != n && 0 <= u.second-'a' && u.second-'a' <= 25) cout << '-' << endl;
}
if(ans[v] != v){
cout << c << endl;
getAns(ans[v], v);
if(cnt != n && 0 <= c-'a' && c-'a' <= 25) cout << '-' << endl;
}
else cnt++;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
nodes[0] = {0, '?'};
for(int i = 0; i<n; i++){
cin >> ts;
addWord(0, -1, 0);
}
calc(0, -1);
cout << dp2[0]-n+1 << endl;
getAns(0, -1);
}
Compilation message
printer.cpp: In function 'void addWord(int, int, int)':
printer.cpp:15:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if(index == ts.length()){
| ~~~~~~^~~~~~~~~~~~~~
printer.cpp: In function 'void getAns(int, int)':
printer.cpp:73:35: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | if(cnt != n && 0 <= c-'a' && c-'a' <= 25) cout << '-' << endl;
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12160 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
25 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
12544 KB |
Output is correct |
2 |
Correct |
49 ms |
12680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
13688 KB |
Output is correct |
2 |
Correct |
262 ms |
15480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
16288 KB |
Output is correct |
2 |
Correct |
97 ms |
13568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
765 ms |
22136 KB |
Output is correct |
2 |
Execution timed out |
1094 ms |
33396 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
635 ms |
20800 KB |
Output is correct |
2 |
Execution timed out |
1091 ms |
37112 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |