답안 #808983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
808983 2023-08-05T13:21:01 Z Boomyday Type Printer (IOI08_printer) C++17
100 / 100
972 ms 60396 KB
//
// Created by adavy on 8/5/2023.
//
//
// Created by adavy on 2/4/2023.
//
//
// Created by adavy on 1/31/2023.
//
// My Header
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!

using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;

#define tcT template<class T
#define tcTU tcT, class U

tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;

// pairs
#define mp make_pair
#define f first
#define s second



#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!

vector<char> toPrint;

struct Trie{
    bool active = 0;
    map<int,Trie> links;
    int numWords=0;
    int wordsTot=0;
    bool special = 0;

    void activate(){
        active = 1;
        numWords = 0;
        wordsTot = 0;
        special = 0;
        //links.rsz(26);
    }

    void dfs(){
        F0R(i, numWords) toPrint.pb('P');
        int specialFound = -1;
        F0R(i, 26){
            if (links.find(i)==links.end()) continue;
            if (links[i].active&&links[i].wordsTot>0){
                if (links[i].special){
                    specialFound =i;
                    continue;
                }
                toPrint.pb('a'+i);
                links[i].dfs();
                toPrint.pb('-');
            }
        }
        if (specialFound!=-1){
            toPrint.pb('a'+specialFound);
            links[specialFound].dfs();
            toPrint.pb('-');
        }

    }

    void add_word(string& s, bool isSpecial = 0){
        special = isSpecial;
        wordsTot++;
        if (s.size()==0){ numWords++; return;}
        int nextChar = s.back()-'a';

        if (links.find(nextChar) == links.end()){
            links[nextChar] = Trie();
        }

        if (links[nextChar].active == 0) links[nextChar].activate();

        s.pop_back();
        links[nextChar].add_word(s,isSpecial);
    }
};


int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    vector<str> words(n);





    Trie t;
    t.activate();
    trav(i, words){ cin >> i; reverse(all(i));}
    std::sort(all(words), []
            (const std::string& first, const std::string& second){
        return first.size() < second.size();
    });
    int bs = words.back().size();
    F0R(i, words.size()-1) t.add_word(words[i]);
    t.add_word(words.back(),1);
    t.dfs();

    cout << toPrint.size()-bs << endl;
    //trav(i, toPrint) cout << i << endl;
    F0R(i, toPrint.size()-bs) cout << toPrint[i] << endl;

}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:47:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
printer.cpp:48:18: note: in expansion of macro 'FOR'
   48 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
printer.cpp:148:5: note: in expansion of macro 'F0R'
  148 |     F0R(i, words.size()-1) t.add_word(words[i]);
      |     ^~~
printer.cpp:47:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
      |                                        ^
printer.cpp:48:18: note: in expansion of macro 'FOR'
   48 | #define F0R(i,a) FOR(i,0,a)
      |                  ^~~
printer.cpp:154:5: note: in expansion of macro 'F0R'
  154 |     F0R(i, toPrint.size()-bs) cout << toPrint[i] << endl;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 9 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 1236 KB Output is correct
2 Correct 20 ms 1512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 3684 KB Output is correct
2 Correct 144 ms 7660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 148 ms 9052 KB Output is correct
2 Correct 43 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 22248 KB Output is correct
2 Correct 803 ms 50816 KB Output is correct
3 Correct 425 ms 27292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 309 ms 17608 KB Output is correct
2 Correct 972 ms 60396 KB Output is correct
3 Correct 487 ms 30968 KB Output is correct
4 Correct 909 ms 57192 KB Output is correct