Submission #528964

# Submission time Handle Problem Language Result Execution time Memory
528964 2022-02-21T20:45:44 Z jeremias Type Printer (IOI08_printer) C++14
0 / 100
71 ms 55616 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 = "";

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++;
                cout << (char)(i + 'a') << "\n";
                if(isWord[trie[node][i]]){
                    cout << "P\n";
                    rta++;
                }
                dfs(trie[node][i]);
                rta++;
                cout << "-\n";
            }
            else
                d_it = i;
        }
    }
    if (d_it != -1){
        rta++;
        cout << (char)(d_it + 'a') << "\n";
        if(isWord[trie[node][d_it]]){
            cout << "P\n";
            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;

    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())
      |            ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 54076 KB Expected integer, but "t" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 54096 KB Expected integer, but "e" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 54024 KB Expected integer, but "h" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 54112 KB Expected integer, but "b" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 54060 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 54132 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 54356 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 54668 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 65 ms 55616 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 71 ms 55348 KB Expected integer, but "a" found
2 Halted 0 ms 0 KB -