#include <bits/stdc++.h>
using namespace std;
const int TOTAL = 25000*20;
vector<vector<int>> nxt(TOTAL, vector<int>(26));
vector<int> cnt(TOTAL);
vector<bool> isTerminal(TOTAL);
string ans = "";
void count(int node)
{
cnt[node] = 1;
for (int i = 0; i < 26; ++i)
{
if (nxt[node][i] != 0)
{
count(nxt[node][i]);
cnt[node] += cnt[nxt[node][i]];
}
}
}
void dfs(int node)
{
if (isTerminal[node])
ans += 'P';
vector<pair<int,int>> que; que.reserve(26);
for (int i = 0; i < 26; ++i)
if (nxt[node][i] != 0)
que.push_back({cnt[nxt[node][i]], i});
sort(que.begin(), que.end());
for (auto& p : que)
{
ans +=(char)('a'+p.second);
dfs(nxt[node][p.second]);
ans += '-';
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
int n; cin >> n;
int MAXID = 0;
ans.reserve(3*n);
while(n--)
{
string s; cin >> s;
int node = 0;
for (char c : s)
{
if (nxt[node][c-'a'] == 0)
nxt[node][c-'a'] = ++MAXID;
node = nxt[node][c-'a'];
}
isTerminal[node] = true;
}
count(0);
dfs(0);
while(ans.back() == '-')
ans.pop_back();
cout << ans.size() << "\n";
for (char c : ans)
cout << c << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
68808 KB |
Output is correct |
2 |
Correct |
43 ms |
68808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
68872 KB |
Output is correct |
2 |
Correct |
48 ms |
68828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
68808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
68872 KB |
Output is correct |
2 |
Correct |
45 ms |
68824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
68844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
68920 KB |
Output is correct |
2 |
Incorrect |
47 ms |
68892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
69100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
69484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
70112 KB |
Output is correct |
2 |
Correct |
142 ms |
71732 KB |
Output is correct |
3 |
Incorrect |
98 ms |
70548 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
69884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |