# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1105321 | Rareshika | Type Printer (IOI08_printer) | C++14 | 138 ms | 106632 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | 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... |