Submission #1300733

#TimeUsernameProblemLanguageResultExecution timeMemory
1300733DaciejMaciejType Printer (IOI08_printer)C++20
100 / 100
107 ms114416 KiB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

template <class T>

using VV = vector<vector<T>>;

using VI = vector<int>;
using VVI = vector<vector<int>>;

using VL = vector<long long>;
using VVL = vector<vector<long long>>;

using VC = vector<char>;
using VVC = vector<vector<char>>;

using VB = vector<bool>;
using VVB = vector<vector<bool>>;

using VD = vector<double>;
using VVD = vector<vector<double>>;

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;

using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;

#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define debug(x) cerr << #x << " = " << x << endl;

const ll MOD = 1e9 + 7;

template <class T>
inline void maxi(T &a, T b)
{
    a = max(a, b);
}

template <class T>
inline void mini(T &a, T b)
{
    a = min(a, b);
}

template <class T>
inline void addi(T &a, T b)
{
    a = (a + b) % MOD;
}

template <class T>
inline void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}

template <class T>
inline T add(T a, T b)
{
    return (a + b) % MOD;
}

template <class T>
inline T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}

template <class T>
inline T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}

constexpr ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }

    return res;
}

const int INF = 1e9;

const int MAX_N = 2e5 + 3;
static const int MAX_W = 1e6 + 5;
struct Trie
{
    int triesz;

    array<array<int, 26>, MAX_W> next;
    array<int, MAX_W> ends;

    Trie() : triesz(0)
    {
        FOR(i, 0, MAX_W)
        {
            next[i].fill(-1);
        }
    }

    void add(string &s)
    {
        int node = 0;
        for (char c : s)
        {
            if (next[node][c - 'a'] != -1)
            {
                node = next[node][c - 'a'];
            }
            else
                node = next[node][c - 'a'] = ++triesz;
        }
        ends[node]++;
    }
};

Trie trie;
string ans = "";

array<int, MAX_W> deep;
array<int, MAX_W> dvert;

void dfs_deep(int node, int d = 0)
{
    deep[node] = 0;
    FOR(i, 0, 26)
    {
        if (int v = trie.next[node][i]; v != -1)
        {
            dfs_deep(v, d + 1);
            if (deep[node] == -1 || deep[node] < deep[v] + 1)
            {
                dvert[node] = i;
                deep[node] = deep[v] + 1;
            }
        }
    }
}

void dfs(int node, char c)
{
    ans.push_back(c);

    FOR(i, 0, trie.ends[node])
    {
        ans.push_back('P');
    }

    FOR(i, 0, 26)
    {
        if (i == dvert[node])
            continue;
        if (int v = trie.next[node][i]; v != -1)
        {
            dfs(v, char('a' + i));
        }
    }

    if (int i = dvert[node]; i != -1)
        dfs(trie.next[node][i], char('a' + i));

    ans.push_back('-');
}

void solve()
{
    dvert.fill(-1);
    deep.fill(0);

    int n;
    cin >> n;

    FOR(i, 0, n)
    {
        string s;
        cin >> s;
        trie.add(s);
    }

    dfs_deep(0);
    dfs(0, 0);
    ans.erase(0, 1);

    while (ans.back() == '-')
        ans.pop_back();

    cout << sz(ans) << LINE;
    for (auto c : ans)
    {
        cout << c << LINE;
    }
}

int main()
{
    fastio();

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...