Submission #1084321

# Submission time Handle Problem Language Result Execution time Memory
1084321 2024-09-05T20:52:00 Z HaciyevAlik Type Printer (IOI08_printer) C++14
100 / 100
165 ms 99608 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define oo 1000000000000000
const int mx = 1e6 + 5;

int trie[mx][26], d[mx], cnt[mx];
int last, node;
vector<char> v;
bool f;
pair<int,int> null = {-1,-1};

void insert(string s) {
    int cur = 0;
    for(int i = 0; i < s.size(); ++i) {
        if(!trie[cur][s[i]-'a']) {
            trie[cur][s[i]-'a'] = ++last;
        }
        cur = trie[cur][s[i]-'a'];
    }
    cnt[cur]++;
}

void dfsd(int u) {
    for(int i = 0; i < 26; ++i) {
        if(trie[u][i]) {
            dfsd(trie[u][i]);    
            d[u] = max(d[u],d[trie[u][i]] + 1);
        }
    }
}

void dfsn(int u) {
    int cur = -1;
    for(int i = 0; i < 26; ++i) {
        if(trie[u][i]) {
            cur = max(cur,d[trie[u][i]]);
        }
    }
    for(int i = 0; i < 26; ++i) {
        if(trie[u][i] > 0 && d[trie[u][i]] == cur) {
            dfsn(trie[u][i]);
        }
    }
    if(cur == -1) {
        node = u;
    }
}

void dfs(int u) {
    if(f) return;
    while(cnt[u] > 0) {
        v.push_back('P');
        cnt[u]--;
    }
    int cur = -1;
    for(int i = 0; i < 26; ++i) {
        if(!trie[u][i]) continue;
        cur = max(cur,d[trie[u][i]]);
    }
    pair<int,int> lastgo = null;
    for(int i = 0; i < 26; ++i) {
        if(!trie[u][i]) continue;
        if(d[trie[u][i]] == cur) lastgo = {trie[u][i],i};
    }
    for(int i = 0; i < 26; ++i) {
        if(trie[u][i] && trie[u][i] != lastgo.first) {
            v.push_back(char('a' + i));
            dfs(trie[u][i]);
        }
    }

    if(lastgo != null) {
        v.push_back(char('a' + lastgo.second));
        dfs(lastgo.first);
    }
    if(u == node) {
        f = 1;
    }
    if(!f) v.push_back('-');
}

signed main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i) {
        string s; cin >> s;
        insert(s);
    }
    dfsd(0); 
    dfsn(0);
    dfs(0);
    cout << v.size() << '\n';
    for(char ch : v) {
        cout << ch << '\n';
    }    
   	return 0;
}

Compilation message

printer.cpp: In function 'void insert(std::string)':
printer.cpp:15:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(int i = 0; i < s.size(); ++i) {
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 3 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5980 KB Output is correct
2 Correct 16 ms 12604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 14816 KB Output is correct
2 Correct 8 ms 3672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 36504 KB Output is correct
2 Correct 151 ms 83916 KB Output is correct
3 Correct 62 ms 43216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 28616 KB Output is correct
2 Correct 165 ms 99608 KB Output is correct
3 Correct 72 ms 49104 KB Output is correct
4 Correct 115 ms 94160 KB Output is correct