#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define tam(s) (int)s.size()
#define all(x) x.begin(), x.end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef vector<int> vi;
typedef pair<int, char> pic;
typedef tuple<int, int, int> tii;
const int lim=500001;
int t[lim], fim[lim];
int32_t main(){
fastio;
int n, k=0, l;
string s, ans="";
cin >> n;
map<char, int> m[lim];
rep(i, 0, n){
cin >> s;
int j=0, p=0;
for(char c: s){
auto it = m[j].find(c);
if(it == m[j].end()) m[j][c] = ++k, j=k;
else j=m[j][c];
t[j] = max(t[j], tam(s)-p);
p++;
}
fim[j]++;
}
auto dfs=[&] (auto self, int i) -> void {
while(fim[i]--){
ans += 'P';
l = tam(ans);
}
if(m[i].empty()) return;
vector<pic> v;
for(auto [a, b] : m[i]) v.eb(t[b], a);
sort(all(v));
for(auto [a, b] : v){
ans += b;
self(self, m[i][b]);
ans += '-';
}
};
dfs(dfs, 0);
cout << l << endl;
rep(i, 0, l) cout << ans[i] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23888 KB |
Output is correct |
2 |
Correct |
21 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23888 KB |
Output is correct |
2 |
Correct |
24 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23888 KB |
Output is correct |
2 |
Correct |
20 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23888 KB |
Output is correct |
2 |
Correct |
21 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23828 KB |
Output is correct |
2 |
Correct |
22 ms |
24144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
24400 KB |
Output is correct |
2 |
Correct |
24 ms |
24656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
27984 KB |
Output is correct |
2 |
Correct |
38 ms |
30288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
30948 KB |
Output is correct |
2 |
Correct |
32 ms |
25168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
38520 KB |
Output is correct |
2 |
Correct |
108 ms |
55300 KB |
Output is correct |
3 |
Correct |
73 ms |
40932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
35832 KB |
Output is correct |
2 |
Correct |
142 ms |
61444 KB |
Output is correct |
3 |
Correct |
81 ms |
42980 KB |
Output is correct |
4 |
Correct |
105 ms |
59240 KB |
Output is correct |