답안 #248503

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
248503 2020-07-12T14:40:58 Z DavidDamian Type Printer (IOI08_printer) C++11
100 / 100
231 ms 52912 KB
#include <bits/stdc++.h>
using namespace std;
///Trie
///Determine the minimum number of operations needed to print all the words in any order
///Just leave the maximum word to the last, then you do not have to come back to 0 letters in the machine
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()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    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:15:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<s.size();i++){
                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1152 KB Output is correct
2 Correct 5 ms 1408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 3456 KB Output is correct
2 Correct 35 ms 6912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 8080 KB Output is correct
2 Correct 11 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 19604 KB Output is correct
2 Correct 201 ms 44464 KB Output is correct
3 Correct 103 ms 23056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 15380 KB Output is correct
2 Correct 231 ms 52912 KB Output is correct
3 Correct 122 ms 26260 KB Output is correct
4 Correct 223 ms 50480 KB Output is correct