This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |