답안 #748991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
748991 2023-05-27T08:47:31 Z LucaLucaM Type Printer (IOI08_printer) C++17
100 / 100
186 ms 106480 KB
#include <bits/stdc++.h>

using namespace std;

/**
Initial costul e suma lungimilor.
Punem fiecare string in trie
Cand coboram in trie daca trb sa afisam, afisam
Cand urcam in trie, dam delete
**/

vector<char>ans;

struct node
{
    int cnt, dep;
    bool term;
    node *f[26];
    node()
    {
        cnt = term = dep = 0;
        memset(f, 0, sizeof(f));
    }
};

struct Trie
{
private:
    node *root = new node;
public:
    Trie(){}
    node *getRoot ()
    {
        return root;
    }
    void add (const string &s)
    {
        node *curr = root;
        for (int i=0; i<(int)s.size(); i++)
        {
            if (curr -> f[s[i] - 'a'] == NULL)
                curr -> f[s[i] - 'a'] = new node;
            curr = curr -> f[s[i] - 'a'];
            curr -> cnt ++;
            curr -> dep = max(curr -> dep, (int)s.size()  - i);
        }
        curr -> term = true;
    }
    void dfs (node *curr)
    {
        if (curr -> term == true)
            ans.push_back('P');
        vector<pair<int, int>>v;

        for (int i=0; i<26; i++)
        {
            if (curr -> f[i] != NULL)
                v.push_back({curr -> f[i] -> dep, i});
        }

        sort(v.begin(), v.end());

        for (int i=0; i<(int)v.size(); i++)
        {
            ans.push_back(char(v[i].second + 'a'));
            dfs(curr -> f[v[i].second]);
        }

        ans.push_back('-');
    }
};

int main()
{
    int n;
    cin >> n;
    Trie T;
    for (int i=1; i<=n; i++)
    {
        string s;
        cin >> s;
        T.add(s);
    }

    T.dfs(T.getRoot());

    while (!ans.empty() && ans.back() == '-')
        ans.pop_back();

    cout << (int)ans.size() << '\n';
    for (const auto &ch : ans)
        cout << ch << '\n';

    return 0;
}

Compilation message

printer.cpp: In constructor 'node::node()':
printer.cpp:21:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   21 |         cnt = term = dep = 0;
      |                      ~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 4 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6244 KB Output is correct
2 Correct 21 ms 13336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 15564 KB Output is correct
2 Correct 11 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 38908 KB Output is correct
2 Correct 146 ms 89412 KB Output is correct
3 Correct 79 ms 46152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 30420 KB Output is correct
2 Correct 186 ms 106480 KB Output is correct
3 Correct 90 ms 52428 KB Output is correct
4 Correct 164 ms 100460 KB Output is correct