#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int mxS = 25000*21;
int n, cnt, dep[mxS];
vector<char> ans;
int trieNode = 0;
int trie[27][mxS];
bitset<mxS> word;
void add(string s){
int v = 0;
for(auto u : s){
int c = u-'a';
if(!trie[c][v])
trie[c][v] = ++trieNode;
v = trie[c][v];
}
word[v]=1; dep[s[0]-'c']=max(dep[s[0]-'c'],sz(s));
}
void dfs(int s){
if(word[s]) ans.pb('P'),cnt++;
vector<int> ord(26,0); iota(all(ord),0);
sort(all(ord),[&](int a, int b){
return dep[a]<dep[b];
});
for(int c : ord){
if(!trie[c][s]) continue;
ans.pb(char('a'+c));
dfs(trie[c][s]);
}
if(cnt!=n) ans.pb('-');
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
string s; cin >> s; add(s);
}
dfs(0); //recur(0);
cout << sz(ans) << "\n";
for(auto u : ans) cout << u << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2516 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
19 ms |
9692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
19980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
16080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |