Submission #248500

# Submission time Handle Problem Language Result Execution time Memory
248500 2020-07-12T14:37:31 Z DavidDamian Type Printer (IOI08_printer) C++11
100 / 100
283 ms 53552 KB
#include <bits/stdc++.h>
using namespace std;
struct node{
    int bucket[27];
    int cnt;
};
node trie[500005];
int act=0;
void trieInsert(string s)
{
    int u=0;
    for(int i=0;i<s.size();i++){
        int letter=s[i]-'a';
        if(trie[u].bucket[letter]==0)
            trie[u].bucket[letter]=++act;
        u=trie[u].bucket[letter];
    }
    trie[u].cnt++;
}
int subtreeSize[500005];
void computeSize(int u)
{
    subtreeSize[u]=1;
    for(int i=0;i<='z'-'a';i++){
        if(trie[u].bucket[i]!=0){
            int v=trie[u].bucket[i];
            computeSize(v);
            subtreeSize[u]=max(subtreeSize[u],1+subtreeSize[v]);
        }
    }
}
struct order
{
    int id,w;
};
bool byW(order a,order b)
{
    return a.w<b.w;
}
string ans;
int printed_words=0;
int n;
void dfs(int u)
{
    order A[27];
    if(trie[u].cnt){
        printed_words++;
        ans.push_back('P');
    }
    for(int i=0;i<='z'-'a';i++){
        A[i].id=i;
        int v=trie[u].bucket[i];
        A[i].w=(v!=0)? subtreeSize[v] : INT_MAX;
    }
    sort(A,A+26,byW);
    for(int i=0;i<='z'-'a';i++){
        if(A[i].w==INT_MAX) continue;
        int v=trie[u].bucket[ A[i].id ];
        if(v!=0){
            ans.push_back(char(A[i].id+'a'));
            dfs(v);
        }
    }
    if(printed_words<n) ans.push_back('-');
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        trieInsert(s);
    }
    computeSize(0);
    dfs(0);
    cout<<ans.size()<<'\n';
    for(char c: ans){
        cout<<c<<'\n';
    }
    return 0;
}

Compilation message

printer.cpp: In function 'void trieInsert(std::__cxx11::string)':
printer.cpp:12:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<s.size();i++){
                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 4 ms 744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 6 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 3328 KB Output is correct
2 Correct 34 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 8200 KB Output is correct
2 Correct 20 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 19856 KB Output is correct
2 Correct 231 ms 44996 KB Output is correct
3 Correct 128 ms 23444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 15636 KB Output is correct
2 Correct 283 ms 53552 KB Output is correct
3 Correct 150 ms 26516 KB Output is correct
4 Correct 246 ms 50348 KB Output is correct