#include <bits/stdc++.h>
using namespace std;
bool vis[2000005], vis1[2000005];
int t[2000005][26];
int t1[2000005][26]; // o --> ch
set<pair<int, int>> v[2000005]; // v[i]--> 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'])
v[o].insert({s.size() - i, s[i] - 'a'}),
t[o][s[i] - 'a'] = ++n, t1[o][s[i] - 'a'] = s.size() - i, o = n;
else
v[o].erase({t1[o][s[i] - 'a'], s[i] - 'a'}),
v[o].insert({(t1[o][s[i] - 'a'] += s.size() - i), s[i] - 'a'}),
o = t[o][s[i] - 'a'];
}
vis1[o] = 1; // 结尾
return;
}
void dfs(int o, vector<char>& ans) {
for (auto i : v[o]) {
ans.push_back(i.second + 'a');
dfs(t[o][i.second], ans);
}
if (vis1[o] == 1)
ans1++, ans.push_back('P');
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);
}
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 |
23 ms |
96860 KB |
Output is correct |
2 |
Correct |
19 ms |
96944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
96860 KB |
Output is correct |
2 |
Correct |
18 ms |
96856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
97208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
96856 KB |
Output is correct |
2 |
Incorrect |
18 ms |
97060 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
97148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
99932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
103004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
113112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
138192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
128720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |