이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
 
using namespace std;
 
const int TOTAL = 25000*20;
 
vector<vector<int>> nxt(TOTAL, vector<int>(26));
vector<int> maxDepth(TOTAL);
vector<bool> isTerminal(TOTAL);
 
string ans = "";
void findMaxDepth(int node, int depth)
{
    maxDepth[node] = depth;
    for (int i = 0; i < 26; ++i)
    {
        if (nxt[node][i] != 0)
        {
            findMaxDepth(nxt[node][i], depth + 1);
            maxDepth[node] = max(maxDepth[node], maxDepth[nxt[node][i]]);
        }
    }
}
 
void dfs(int node)
{
    if (isTerminal[node])
        ans += 'P';
    vector<pair<int,int>> que; que.reserve(26);
    for (int i = 0; i < 26; ++i)
        if (nxt[node][i] != 0)
            que.push_back({maxDepth[nxt[node][i]], i});
    sort(que.begin(), que.end());
    
    for (auto& p : que)
    {
        ans +=(char)('a'+p.second);
        dfs(nxt[node][p.second]);
        ans += '-';
    }
}
 
int main() 
{
    ios::sync_with_stdio(0), cin.tie(0);
    int n; cin >> n;
    int MAXID = 0;
    ans.reserve(3*n);
    while(n--)
    {
        string s; cin >> 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;
    }
    findMaxDepth(0,0);
    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... |