Submission #498638

#TimeUsernameProblemLanguageResultExecution timeMemory
498638_AnKahngType Printer (IOI08_printer)C++14
100 / 100
143 ms99572 KiB
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

int n,mx;
string s,larger;
vector<char> kq;

struct node{
    node* child[26];
    bool fl,e;
    node(){
        fo(i,0,25) child[i] = nullptr;
        e = fl = false;
    }
};

node* root;

struct Trie{
    void Insert(string s, bool o){
        node* p = root;
        for(char a : s){
            if(!p -> child[a -'a']) p -> child[a - 'a'] = new node;
            p = p -> child[a - 'a'];
            p -> fl = o;
        }
        p -> fl = o;
        p -> e = true;
    }
    void Dfs(node *p){
        if(p -> e) kq.push_back('P');
        int tmp = -1;
        fo(i,0,25)
            if(p -> child[i]) {
                node* v = p -> child[i];
                if(v -> fl){
                    tmp = i;
                    continue;
                }
                kq.push_back('a' + i);
                Dfs(v);
                kq.push_back('-');
            }
        if(tmp != -1){
            kq.push_back('a' + tmp);
            Dfs(p -> child[tmp]);
        }
    }
} trie;

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    root = new node;
    cin >> n;
    fo(i,1,n) {
        cin >> s;
        trie.Insert(s,0);
        if(s.size() > mx){
            mx = s.size();
            larger = s;
        }
    }
    trie.Insert(larger,1);
    trie.Dfs(root);
    cout << kq.size()<<"\n";
    for(auto k : kq) cout << k<<"\n";






}

Compilation message (stderr)

printer.cpp: In function 'int main()':
printer.cpp:60:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         if(s.size() > mx){
      |            ~~~~~~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...