Submission #1105321

# Submission time Handle Problem Language Result Execution time Memory
1105321 2024-10-26T07:09:33 Z Rareshika Type Printer (IOI08_printer) C++14
100 / 100
138 ms 106632 KB
#ifndef LOCAL
    #pragma GCC optimize("O3")
    #pragma GCC target("avx2")
#endif

#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;

using namespace std;

/*******************************/
// INPUT / OUTPUT

/*******************************/
/// GLOBAL DECLARATIONS

int N;

struct Trie
{
    int cnt, fin, lant_maxim, lit_best = -1;
    Trie* fii[26];
};

Trie *root;

vector <char> ops;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    cin >> N;
}
///-------------------------------------
inline Trie* adauga(Trie* trie, const string &cuv, int idx)
{
    trie->cnt ++;
    if (idx == cuv.length())
    {
        trie->fin ++;
        return trie;
    }

    // cerr << idx << " " << cuv[idx] - 'a' << "\n";
    if (trie->fii[cuv[idx] - 'a'] == nullptr)
        trie->fii[cuv[idx] - 'a'] = new Trie();
    
    trie->fii[cuv[idx] - 'a'] = adauga(trie->fii[cuv[idx] - 'a'], cuv, idx + 1);
    if (trie->lant_maxim < trie->fii[cuv[idx] - 'a']->lant_maxim + 1)
    {
        trie->lant_maxim = trie->fii[cuv[idx] - 'a']->lant_maxim + 1;
        trie->lit_best = cuv[idx] - 'a';
    }

    return trie;
}
///-------------------------------------
inline void dfs(Trie* trie, bool nebun=false)
{
    if (trie->fin > 0)
        ops.push_back('P');
    
    for (int ch = 0 ; ch < 26 ; ++ ch)
    {
        if (ch == trie->lit_best) continue;

        if (trie->fii[ch] != nullptr && trie->fii[ch]->cnt > 0)
        {
            ops.push_back('a' + ch);
            dfs(trie->fii[ch]);
        }
    }

    if (trie->lit_best != -1)
    {
        ops.push_back('a' + trie->lit_best);
        dfs(trie->fii[trie->lit_best], nebun);
    }
    

    if (!nebun)
        ops.push_back('-');
}
///-------------------------------------
inline void Solution()
{
    root = new Trie();
    
    string cuv;
    for (int i = 0 ; i < N ; ++ i)
    {
        cin >> cuv;
        root = adauga(root, cuv, 0);
    }

    dfs(root, true);
}
///-------------------------------------
inline void Output()
{
    cout << ops.size() << "\n";
    for (auto op: ops)
        cout << op << "\n";
}
///-------------------------------------
int main()
{
    #ifndef LOCAL
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif
    ReadInput();
    Solution();
    Output();
    return 0;
}

Compilation message

printer.cpp: In function 'Trie* adauga(Trie*, const string&, int)':
printer.cpp:52:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     if (idx == cuv.length())
      |         ~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 2 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1872 KB Output is correct
2 Correct 4 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6480 KB Output is correct
2 Correct 21 ms 13392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15884 KB Output is correct
2 Correct 8 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 39080 KB Output is correct
2 Correct 115 ms 89384 KB Output is correct
3 Correct 58 ms 46272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 30720 KB Output is correct
2 Correct 138 ms 106632 KB Output is correct
3 Correct 73 ms 52332 KB Output is correct
4 Correct 131 ms 100548 KB Output is correct