답안 #528984

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528984 2022-02-21T20:58:28 Z jeremias Type Printer (IOI08_printer) C++14
100 / 100
116 ms 59136 KB
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define all(x) x.begin(), x.end()
#define LSB(x) x & -x
typedef long long ll;
using namespace std;

/**
Trie
traverse in sorted order
**/

int trie[500020][27], d[500020], nxt = 1, rta = 0;
bool isWord[500020], dont[500020];
string longest = "", ans = "";

void insert(string &s)
{
    int i = 0, v = 0;
    while (i < s.size())
    {
        if (trie[v][(s[i] - 'a')] == -1)
        {
            trie[v][(s[i] - 'a')] = nxt++;
        }
        v = trie[v][(s[i++] - 'a')];
    }
    isWord[v] = true;
}

void depths(int node, int depth)
{
    d[node] = depth;
    forn(i, 26)
    {
        if (trie[node][i] != -1)
        {
            depths(trie[node][i], depth + 1);
        }
    }
}

void dfs(int node)
{
    int d_it = -1;
    forn(i, 26)
    {
        if (trie[node][i] != -1)
        {
            if (!dont[trie[node][i]])
            {
                rta++;
                ans += (char)(i + 'a');
                if (isWord[trie[node][i]])
                {
                    ans += 'P';
                    rta++;
                }
                dfs(trie[node][i]);
                rta++;
                ans += '-';
            }
            else
                d_it = i;
        }
    }
    if (d_it != -1)
    {
        rta++;
        ans += (char)(d_it + 'a');
        if (isWord[trie[node][d_it]])
        {
            ans += 'P';
            rta++;
        }
        dfs(trie[node][d_it]);
    }
}

int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    memset(trie, -1, sizeof(trie));
    memset(isWord, false, sizeof(isWord));
    memset(dont, false, sizeof(dont));
    forn(i, n)
    {
        string s;
        cin >> s;
        insert(s);
        if (s.size() > longest.size())
        {
            longest = s;
        }
    }
    int v = 0;
    forn(i, longest.size())
    {
        v = trie[v][longest[i] - 'a'];
        dont[v] = true;
    }
    // declare depths
    depths(0, 0);
    // dfs
    dfs(0);
    // display
    cout << rta << "\n";
    for (auto x : ans)
        cout << x << "\n";
    return 0;
}

/*
 */

Compilation message

printer.cpp: In function 'void insert(std::string&)':
printer.cpp:21:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     while (i < s.size())
      |            ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 54092 KB Output is correct
2 Correct 22 ms 54076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 54128 KB Output is correct
2 Correct 24 ms 54104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 54092 KB Output is correct
2 Correct 21 ms 54092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 54092 KB Output is correct
2 Correct 22 ms 54088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 54056 KB Output is correct
2 Correct 24 ms 54180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 54220 KB Output is correct
2 Correct 25 ms 54200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 54408 KB Output is correct
2 Correct 33 ms 54840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 54908 KB Output is correct
2 Correct 29 ms 54468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 55784 KB Output is correct
2 Correct 101 ms 58364 KB Output is correct
3 Correct 64 ms 56548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 55516 KB Output is correct
2 Correct 116 ms 59136 KB Output is correct
3 Correct 70 ms 56828 KB Output is correct
4 Correct 108 ms 58876 KB Output is correct