Submission #674036

# Submission time Handle Problem Language Result Execution time Memory
674036 2022-12-22T16:38:49 Z Hacv16 Type Printer (IOI08_printer) C++17
100 / 100
92 ms 55508 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MAX = 5e5 + 15;
const int ALP = 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"

int n, trie[MAX][ALP], mark[MAX], cnt, numWords;
bool fim[MAX];

string Maxs; 

vector<char> ans;

void Add(string& s){
    int cur = 0;

    for(int i = 0; i < sz(s); i++){
        int id = s[i] - 'a';

        if(trie[cur][id] == 0)
            trie[cur][id] = ++cnt;

        cur = trie[cur][id];
    }

    fim[cur] = true;
}

void Done(){
    cout << sz(ans) << '\n';

    for(auto c : ans)
        cout << c << '\n';

    exit(0);
}

void Traverse(string& s){
    int cur = 0;

    for(int i = 0; i < sz(s); i++){
        int id = s[i] - 'a';
        cur = trie[cur][id];

        mark[cur] = true;
    }
}

void dfs(int u, char c = '*', bool f = false){
    if(f) ans.pb(c); 

    if(fim[u]) ans.pb('P'), numWords++;
    if(numWords == n) Done();

    int left = -1;
    char aux = '*';

    for(char c = 'a'; c <= 'z'; c++){
        int id = c - 'a';

        if(trie[u][id] == 0)
            continue;

        if(mark[trie[u][id]]) 
            left = trie[u][id], aux = c;
        else 
            dfs(trie[u][id], c, true);
    }

    if(left != -1) dfs(left, aux, true);
    if(f) ans.pb('-');
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 

    cin >> n;

    for(int i = 0; i < n; i++){
        string s; cin >> s;
        Add(s);

        if(sz(s) > sz(Maxs))
            Maxs = s;
    }

    Traverse(Maxs);

    dfs(0);

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 440 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1104 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3540 KB Output is correct
2 Correct 12 ms 7124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 8408 KB Output is correct
2 Correct 6 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 20492 KB Output is correct
2 Correct 75 ms 46664 KB Output is correct
3 Correct 48 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 16164 KB Output is correct
2 Correct 92 ms 55508 KB Output is correct
3 Correct 48 ms 27596 KB Output is correct
4 Correct 88 ms 52424 KB Output is correct