Submission #802987

# Submission time Handle Problem Language Result Execution time Memory
802987 2023-08-02T17:20:17 Z huantran Type Printer (IOI08_printer) C++17
100 / 100
170 ms 230596 KB
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <vector>
#include <map>
#define bit(x, i) ((x >> i) & 1LL)

using namespace std;
typedef long long int ll;
const int maxn = 1e6 + 5;

int n, num = 0;
vector<char> ans;

struct Vertex{
    ll next[27];
    ll output = 0;  
    ll sz;
};

Vertex trie[maxn];
ll cnt = 1;
void add_edge(string s)
{
    ll u = 0;
    for(auto c : s)
    {
        if(!trie[u].next[c - 'a'])
        {
            trie[u].next[c - 'a'] = cnt++;
        }
        u = trie[u].next[c - 'a'];   
    } 
    trie[u].output++;   
}

void trie_size(int u)
{
    trie[u].sz = 1;
    for(int i = 0; i <= 25; i++)
    {
        if(trie[u].next[i] != 0)
        {
            trie_size(trie[u].next[i]);
            trie[u].sz = max(trie[u].sz, trie[trie[u].next[i]].sz + 1);
        }
    }
}

void get_ans(int u)
{
    if(trie[u].output)
    {
        for(int i = 1; i <= trie[u].output && num < n; i++)
        {
            ans.push_back('P');
            num++;
        }
    }

    vector<pair<ll, pair<ll, ll>>> e;
    for(int i = 0; i <= 25; i++)
    {
        if(trie[u].next[i] != 0)
            e.push_back({trie[trie[u].next[i]].sz, {trie[u].next[i], i}});
    }
    sort(e.begin(), e.end());

    for(int i = 0; i < e.size(); i++)
    {
        ans.push_back((char)('a' + e[i].second.second));
        get_ans(e[i].second.first);
        if(num < n)
            ans.push_back('-');
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    ll leng = 0;
    string t;
    for(int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        add_edge(s);
    }

    trie_size(0);
    get_ans(0);
    cout << ans.size() << '\n';
    for(auto j : ans)
        cout << j << '\n';
}

Compilation message

printer.cpp: In function 'void get_ans(int)':
printer.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < e.size(); i++)
      |                    ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:85:8: warning: unused variable 'leng' [-Wunused-variable]
   85 |     ll leng = 0;
      |        ^~~~
# Verdict Execution time Memory Grader output
1 Correct 70 ms 227248 KB Output is correct
2 Correct 68 ms 227192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 227260 KB Output is correct
2 Correct 72 ms 227288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 227300 KB Output is correct
2 Correct 69 ms 227252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 227276 KB Output is correct
2 Correct 78 ms 227292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 227276 KB Output is correct
2 Correct 70 ms 227312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 227316 KB Output is correct
2 Correct 70 ms 227396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 227504 KB Output is correct
2 Correct 81 ms 227748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 227912 KB Output is correct
2 Correct 81 ms 227596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 228592 KB Output is correct
2 Correct 160 ms 230104 KB Output is correct
3 Correct 118 ms 229028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 228416 KB Output is correct
2 Correct 170 ms 230596 KB Output is correct
3 Correct 120 ms 229192 KB Output is correct
4 Correct 147 ms 230364 KB Output is correct