This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |