Submission #639885

# Submission time Handle Problem Language Result Execution time Memory
639885 2022-09-12T15:44:50 Z PoonYaPat Type Printer (IOI08_printer) C++14
100 / 100
128 ms 99588 KB
#include <bits/stdc++.h>
using namespace std;

int n,mmax;
bool havemax=false;
const int sz=26;
vector<char> ans;

struct Trienode {
    struct Trienode *children[26];
    int level;
    char x;
    bool Endword,longest;
};

struct Trienode *getnode(char x, int l) {
    struct Trienode *pnode = new  Trienode;
    for (int i=0; i<sz; ++i) pnode->children[i]=NULL;
    pnode->level=l;
    pnode->x=x;
    pnode->Endword=false;
    pnode->longest=false;
    return pnode;
};

void insert(struct Trienode *root, string key) {
    struct Trienode *pnode = root;

    for (int i=0; i<key.size(); ++i) {
        int idx=key[i]-'a';
        if (!pnode->children[idx]) pnode->children[idx]=getnode(key[i],pnode->level+1);

        pnode=pnode->children[idx];
    }
    pnode->Endword=true;
}

bool fm(struct Trienode *root) { //if it has the longest path, it will return true;
    if (root->level==mmax && !havemax) {
        havemax=true;
        root->longest=true;
        return true;
    }
    for (int i=0; i<sz; ++i) if (root->children[i] && fm(root->children[i])) {
        root->longest=true;
        return true;
    }
    return false;
}

void dfs(struct Trienode *root) {
    if (root->x != '-') ans.push_back(root->x);
    if (root->Endword) ans.push_back('P');

    struct Trienode *pnode;
    bool haveP=false;

    for (int i=0; i<sz; ++i) if (root->children[i]) {
        if (!root->children[i]->longest) dfs(root->children[i]);
        else pnode=root->children[i], haveP=true;
    }

    if (haveP) dfs(pnode);

    if (!root->longest) ans.push_back('-');
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    struct Trienode *root = getnode('-',0);
    while (n--) {
        string s; cin>>s;
        mmax=max(mmax,(int)(s.size()));
        insert(root,s);
    }
    fm(root);
    dfs(root);
    cout<<ans.size()<<"\n";
    for (auto s : ans) cout<<s<<"\n";
}

Compilation message

printer.cpp: In function 'void insert(Trienode*, std::string)':
printer.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i=0; i<key.size(); ++i) {
      |                   ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1748 KB Output is correct
2 Correct 4 ms 2272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 5972 KB Output is correct
2 Correct 16 ms 12548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 14760 KB Output is correct
2 Correct 7 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 36616 KB Output is correct
2 Correct 110 ms 83752 KB Output is correct
3 Correct 64 ms 43140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 28620 KB Output is correct
2 Correct 128 ms 99588 KB Output is correct
3 Correct 74 ms 49112 KB Output is correct
4 Correct 125 ms 94008 KB Output is correct