Submission #1312608

#TimeUsernameProblemLanguageResultExecution timeMemory
1312608algoproclubType Printer (IOI08_printer)C++20
100 / 100
658 ms99052 KiB
// UUID: af71cda5-df6d-4d7b-a427-fea71659c16d
#include <bits/stdc++.h>
using namespace std;

struct abctree {
    abctree* next[26];
    bool end;
    bool inthelongest;
    abctree() {
        for (int i = 0; i < 26; i++) next[i] = nullptr;
        end = false;
        inthelongest = false;
    }
};

abctree* mainroot;
vector<char> out;
string longest = "";
void add(abctree* root, string s) {
    abctree* cur = root;
    for (char c:s) {
        int index = c - 'a';
        if (!cur -> next[index]) {
            cur -> next[index] = new abctree;
        }
        cur = cur -> next[index];
    }
    cur -> end = true;
}

void print(abctree* root) {
    abctree* cur = root;
    if (cur -> end) {
        out.push_back('P');
    }
    for (int i = 0;i < 26;i++) {
        if (cur -> next[i] and !cur -> next[i] -> inthelongest) {
            out.push_back(char(i + 'a'));
            //if (cur -> next[i] -> end) out.push_back('P');
            print(cur -> next[i]);
        }
    }
    for (int i = 0;i < 26;i++) {
        if (cur -> next[i] and cur -> next[i] -> inthelongest) {
            out.push_back(char(i + 'a'));
            //if (cur -> next[i] -> end) out.push_back('P');
            print(cur -> next[i]);
        }
    }
    /*if (cur -> end) {
        out.push_back('P');
    }*/
    if (mainroot != cur) {
        out.push_back('-');
    }
    
}

int main() {
	int n;cin >> n;
    mainroot = new abctree;
    string s;
	for (int i = 0;i < n;i++) {
        cin >> s;
        if (s.size() > longest.size()) longest = s;
        add(mainroot, s);
	}
    abctree* cur = mainroot;
    mainroot -> inthelongest = true;
    for (char c:longest) {
        cur = cur -> next[c - 'a'];
        cur -> inthelongest = true;
    }
    print(mainroot);
    cout << out.size() - longest.size() << endl;
    for (int i = 0;i < out.size() - longest.size();i++) {
        cout << out[i] << endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...