#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]);
ans += '-';
}
}
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);
int trash = (int)ans.size() - 1;
while(ans[trash] == '-')trash--;
ans = ans.substr(0, trash + 1);
cout << (int)ans.size() << endl;
for(int i = 0; i < (int)ans.size(); ++i){
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1152 KB |
Output is correct |
2 |
Correct |
6 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1920 KB |
Output is correct |
2 |
Correct |
7 ms |
2176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
4096 KB |
Output is correct |
2 |
Correct |
18 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
8584 KB |
Output is correct |
2 |
Correct |
12 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
19784 KB |
Output is correct |
2 |
Correct |
101 ms |
44232 KB |
Output is correct |
3 |
Correct |
60 ms |
23948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
15624 KB |
Output is correct |
2 |
Correct |
114 ms |
52412 KB |
Output is correct |
3 |
Correct |
66 ms |
27092 KB |
Output is correct |
4 |
Correct |
101 ms |
49516 KB |
Output is correct |