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>
 
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 | 
|---|
| 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... |