#include <bits/stdc++.h>
using namespace std;
const int TOTAL = 25000*20;
vector<vector<int>> nxt(TOTAL, vector<int>(26));
vector<bool> isTerminal(TOTAL);
string ans = "";
const int INF = 1e9+7;
void dfs(int node)
{
if (isTerminal[node])
ans += 'P';
int markedEdge = INF;
for (int i = 0; i < 26; ++i)
{
if (nxt[node][i] != 0)
{
if (nxt[node][i] < 0)
markedEdge = i;
else
{
ans +=(char)('a'+i);
dfs(nxt[node][i]);
ans += '-';
}
}
}
if (markedEdge != INF)
{
ans +=(char)('a'+markedEdge);
dfs(-nxt[node][markedEdge]);
ans += '-';
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
int n; cin >> n;
int MAXID = 0;
ans.reserve(3*n);
string longest = "";
while(n--)
{
string s; cin >> s;
if (s.length() > longest.length())
longest = 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;
}
{
int node = 0;
for (char c : longest)
{
nxt[node][c-'a'] *= -1;
node = -nxt[node][c-'a'];
}
}
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 |
43 ms |
66892 KB |
Output is correct |
2 |
Correct |
49 ms |
66828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
66836 KB |
Output is correct |
2 |
Correct |
41 ms |
66840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
66892 KB |
Output is correct |
2 |
Correct |
45 ms |
66864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
66892 KB |
Output is correct |
2 |
Correct |
44 ms |
66848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
66900 KB |
Output is correct |
2 |
Correct |
44 ms |
66932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
66960 KB |
Output is correct |
2 |
Correct |
52 ms |
66888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
67032 KB |
Output is correct |
2 |
Correct |
55 ms |
67276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
67436 KB |
Output is correct |
2 |
Correct |
52 ms |
67020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
67988 KB |
Output is correct |
2 |
Correct |
101 ms |
69272 KB |
Output is correct |
3 |
Correct |
93 ms |
68060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
67768 KB |
Output is correct |
2 |
Correct |
116 ms |
69600 KB |
Output is correct |
3 |
Correct |
87 ms |
68268 KB |
Output is correct |
4 |
Correct |
110 ms |
69524 KB |
Output is correct |