#include <bits/stdc++.h>
#define fu(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
const int N = 25e3 + 10;
const int M = 5e5 + 10;
int n;
string a[N], res;
int trie[M][27], sz = 0;
bool tail[M];
string longest;
void Insert(const string &a)
{
int node = 0;
for (char c : a)
{
int x = int(c) - 96;
if (!trie[node][x]) trie[node][x] = ++sz;
node = trie[node][x];
}
tail[node] = true;
}
void DFS(int u, int depth, bool still)
{
int c = int(longest[depth]) - 96;
// cout << u << " " << depth << "\n";
fu(i, 1, 26)
{
if (trie[u][i] == 0) continue;
if (i == c && still) continue;
// cout << char(i + 96) << "\n";
res.push_back(char(i + 96));
if (tail[trie[u][i]])
{
// cout << "P\n";
res.push_back('P');
}
DFS(trie[u][i], depth + 1, false);
// cout << "-\n";
res.push_back('-');
}
if (still)
{
// cout << longest[depth] << "\n";
res.push_back(longest[depth]);
if (tail[trie[u][c]]) res.push_back('P');
if (depth < (int)longest.size() - 1 && trie[u][c]) DFS(trie[u][c], depth + 1, still);
}
}
int main()
{
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n;
fu(i, 1, n)
{
cin >> a[i];
}
fu(i, 1, n)
{
Insert(a[i]);
}
longest = a[1];
fu(i, 2, n)
{
if (longest.size() < a[i].size()) longest = a[i];
}
DFS(0, 0, 1);
cout << (int)res.size() << "\n";
fu(i, 0, (int)res.size() - 1)
{
cout << res[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1120 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
3 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1868 KB |
Output is correct |
2 |
Correct |
4 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
3916 KB |
Output is correct |
2 |
Correct |
17 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
8408 KB |
Output is correct |
2 |
Correct |
9 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
19508 KB |
Output is correct |
2 |
Correct |
112 ms |
44160 KB |
Output is correct |
3 |
Correct |
60 ms |
23904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
15200 KB |
Output is correct |
2 |
Correct |
129 ms |
52412 KB |
Output is correct |
3 |
Correct |
71 ms |
26976 KB |
Output is correct |
4 |
Correct |
114 ms |
49504 KB |
Output is correct |