제출 #1139094

#제출 시각아이디문제언어결과실행 시간메모리
1139094ymorasType Printer (IOI08_printer)C++17
80 / 100
228 ms28240 KiB
#include <bits/stdc++.h>
using namespace std;

const int ALPHABET = 26;
const int MAXN = 2e5 + 5;

int trie[MAXN][ALPHABET];
int endW[MAXN];
int marked[MAXN];
int nxt = 1;
vector<char> operations;

void insert(const string& s) {
    int node = 0;
    for (char c : s) {
        int ch = c - 'a';
        if (trie[node][ch] == 0) {
            trie[node][ch] = nxt++;
        }
        node = trie[node][ch];
    }
    endW[node] = 1;
}

void lastPath(const string& s) {
    int node = 0;
    for (char c : s) {
        int ch = c - 'a';
        node = trie[node][ch];
        marked[node] = 1;
    }
}

bool visited[MAXN][ALPHABET];
void dfs(int node) {

    // Primero procesar nodos no marcados
    for (int c = 0; c < ALPHABET; c++) {
        if (trie[node][c] == 0) continue;
        int child = trie[node][c];

        if (!marked[child]) {
        visited[node][c] = 1;

            operations.push_back(c + 'a');
            
            if (endW[child]) {
                operations.push_back('P');

            }
            if (!visited[child][c]) { 
                dfs(child);
                
            }
            
            operations.push_back('-');
        } 
    }
    for (int c = 0; c < ALPHABET; c++) {
        if (trie[node][c] == 0) continue;
        int child = trie[node][c];
        
        if (marked[child]) {
        visited[node][c] = 1;
            operations.push_back(c + 'a');
            if (endW[child]) {
                    operations.push_back('P');
                }
            if (!visited[child][c]){
                dfs(child);
                
            }
            
        }
    }

}


int main() {

    int N;
    cin >> N;
    
    memset(trie, 0, sizeof(trie));
    memset(endW, 0, sizeof(endW));
    memset(marked, 0, sizeof(marked));
    nxt = 1;
    operations.clear();
    
    vector<string> words(N);
    string longestWord;
    
    for (int i = 0; i < N; i++) {
        cin >> words[i];
        insert(words[i]);
        
        if (words[i].length() > longestWord.length()) {
            longestWord = words[i];
        }
    }
    
    lastPath(longestWord);
    
    dfs(0);
    
    cout << operations.size() << endl;
    for (char op : operations) {
        cout << op << endl;
    }
    return 0;
}
#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...