#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100, M = 25005;
int codes[N][26], cntEnd[N * 26];
bool marked[N * 26];
int n, tries, maxLength, maxString;
string ans, s[M];
void add(string &s, bool mark){
int code = 0;
for(int i = 0; i < (int)s.size(); ++i){
int c = s[i] - 'a';
if(!codes[code][c]){
codes[code][c] = ++tries;
}
code = codes[code][c];
marked[code] = mark;
}
cntEnd[code] += !mark;
}
void findMinTime(int code){
for(int i = 0; i < cntEnd[code]; ++i)ans += 'P';
int last = -1;
for(int i = 0; i < 26; ++i){
if(!codes[code][i])continue;
if(marked[codes[code][i]]){
last = i;
}else{
ans += char(i + 'a');
findMinTime(codes[code][i]);
ans += '-';
}
}
if(~last){
ans += char(last + 'a');
findMinTime(codes[code][last]);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> s[i];
add(s[i], 0);
if(maxLength < (int)s[i].size()){
maxLength = (int)s[i].size();
maxString = i;
}
}
add(s[maxString], 1);
findMinTime(0);
cout << (int)ans.size() << endl;
for(int i = 0; i < (int)ans.size(); ++i){
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1152 KB |
Output is correct |
2 |
Correct |
6 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1920 KB |
Output is correct |
2 |
Correct |
7 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
4096 KB |
Output is correct |
2 |
Correct |
18 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
8584 KB |
Output is correct |
2 |
Correct |
12 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
19732 KB |
Output is correct |
2 |
Correct |
98 ms |
43956 KB |
Output is correct |
3 |
Correct |
55 ms |
23696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
15508 KB |
Output is correct |
2 |
Correct |
114 ms |
52016 KB |
Output is correct |
3 |
Correct |
63 ms |
26644 KB |
Output is correct |
4 |
Correct |
103 ms |
49200 KB |
Output is correct |