#include <bits/stdc++.h>
using namespace std;
bool vis1[500005];
int t[500005][26];//o --> ch
int t2[500005];//i的深度
set<pair<int,int>> t1[500005];//i的子树的深度排序后
int k,ans1=0;
struct Trie {
int n = 1;
void update(string s) {
int o = 1;
for (int i = 0; i < (int)s.size(); i++) {
if (!t[o][s[i] - 'a'])
t[o][s[i] - 'a'] = ++n, o = n;
else
o = t[o][s[i] - 'a'];
}
vis1[o] = 1; // 结尾
return;
}
int dfs1(int o) {
int flag=0;
for(int i=0;i<26;i++){
if(t[o][i]!=0){
flag=1;
int d=dfs1(t[o][i]);//o子树i的深度
t1[o].insert({d,i});
t2[o]+=d;
}
}
if(!flag)return 1;
return t2[o];
}
void dfs(int o, vector<char>& ans) {
if(vis1[o]==1)ans1++,ans.push_back('P');
for(auto i:t1[o]){
ans.push_back(i.second+'a');
dfs(t[o][i.second],ans);
}
if(ans1!=k)ans.push_back('-');
return;
}
};
int main() {
cin >> k;
string s;
Trie tr;
for (int i = 0; i < k; i++) {
cin >> s;
tr.update(s);
}
tr.dfs1(1);
vector<char> ans;
tr.dfs(1, ans);
cout << ans.size();
cout << "\n";
for (char i : ans)
cout << i << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
25176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
25180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
25180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
26204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
29148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
35032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
50128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
44744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |