#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, longest;
struct Node
{
Node *child[26];
bool tail;
Node()
{
fu(i, 0, 25) child[i] = NULL;
tail = false;
}
};
Node *root;
void Insert(const string &a)
{
Node *p = root;
for (char x : a)
{
int v = int(x) - 97;
if (p->child[v] == NULL) p->child[v] = new Node();
p = p->child[v];
}
p->tail = true;
}
void DFS(Node *u, int depth, bool still)
{
int c = int(longest[depth]) - 97;
fu(i, 0, 25)
{
if (u->child[i] == NULL) continue;
if (i == c && still) continue;
res.push_back(char(i + 97));
if (u->child[i]->tail) res.push_back('P');
DFS(u->child[i], depth + 1, false);
res.push_back('-');
}
if (still)
{
res.push_back(longest[depth]);
if (u->child[c]->tail) res.push_back('P');
if (depth < (int)longest.size() - 1 && u->child[c] != NULL) DFS(u->child[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];
}
root = new Node();
fu(i, 1, n)
{
Insert(a[i]);
}
longest = a[1];
fu(i, 2, n)
{
if (longest.size() < a[i].size()) longest = a[i];
}
DFS(root, 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 |
972 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 |
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 |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2636 KB |
Output is correct |
2 |
Correct |
5 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6732 KB |
Output is correct |
2 |
Correct |
21 ms |
13268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
15444 KB |
Output is correct |
2 |
Correct |
9 ms |
4172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
37372 KB |
Output is correct |
2 |
Correct |
141 ms |
85116 KB |
Output is correct |
3 |
Correct |
74 ms |
44520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
29152 KB |
Output is correct |
2 |
Correct |
168 ms |
101116 KB |
Output is correct |
3 |
Correct |
89 ms |
50472 KB |
Output is correct |
4 |
Correct |
147 ms |
95404 KB |
Output is correct |