답안 #444019

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444019 2021-07-13T01:57:54 Z cpp219 Type Printer (IOI08_printer) C++14
100 / 100
206 ms 99060 KB
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e5 + 9;
const ll Log2 = 20;
const ll inf = 1e9 + 7;

deque<char> ans;
string s;
ll n;

struct Trie{
    Trie *child[26]; ll End,mx;
    Trie (){
        for (ll i = 0;i < 26;i++) child[i] = NULL;
        End = 0; mx = 0;
    }
    void Add(ll pos,string &s){
        if (pos == s.size()){
            End++; mx = max(mx,1); return;
        }
        ll now = s[pos] - 'a';
        if (!child[now]) child[now] = new Trie;
        child[now] -> Add(pos + 1,s);
        mx = max(mx,child[now] -> mx + 1);
    }
    void DFS(){
        while(End--) ans.push_back('P');
        ll cur = 0,chosen = 6969;
        for (ll i = 0;i < 26;i++) if (child[i] && cur < child[i] -> mx)
            cur = child[i] -> mx, chosen = i;
        for (ll i = 0;i < 26;i++){
            if (child[i] && i != chosen) ans.push_back(i + 'a'),child[i] -> DFS();
        }
        if (chosen < 26 && child[chosen]) ans.push_back(chosen + 'a'),child[chosen] -> DFS();
        ans.push_back('-');
    }
};

Trie Tree;

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>n;
    for (ll i = 1;i <= n;i++){
        cin>>s; Tree.Add(0,s);
    }
    Tree.DFS(); //return 0;
    while(ans.back() == '-') ans.pop_back();
    cout<<ans.size()<<"\n";
    for (auto i : ans) cout<<i<<"\n";
}

Compilation message

printer.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization O2
      | 
printer.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
printer.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    3 | #pragma target ("avx2")
      | 
printer.cpp: In member function 'void Trie::Add(int, std::string&)':
printer.cpp:27:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         if (pos == s.size()){
      |             ~~~~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1740 KB Output is correct
2 Correct 4 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 5796 KB Output is correct
2 Correct 26 ms 12356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 14492 KB Output is correct
2 Correct 10 ms 3148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 36236 KB Output is correct
2 Correct 172 ms 83220 KB Output is correct
3 Correct 94 ms 42716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 28372 KB Output is correct
2 Correct 206 ms 99060 KB Output is correct
3 Correct 106 ms 48480 KB Output is correct
4 Correct 195 ms 93556 KB Output is correct