Cod sursa(job #2487014)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 3 noiembrie 2019 19:33:52
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb

#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")

ifstream in("trie.in");
ofstream out("trie.out");

struct node {
    node* mp[27];
    int signs = 0, exists = 0;
};

int main()
{
    //cout << "Hello world!" << endl;

    node root;

    int op; string cnt;
    while(in >> op >> cnt) {
        if(op == 0) {
            node *self = &root;
            for(int i = 0; i < cnt.size(); i++) {
                self = self->mp[cnt[i] - 'a'];
                self->exists++;
            }
            self->signs++;
        }
        if(op == 1) {
            node *self = &root;
            for(int i = 0; i < cnt.size(); i++) {
                self = (self->mp[cnt[i] - 'a']);
                self->exists--;
            }
            self->signs--;
        }
        if(op == 2) {
            node *self = &root;
            for(int i = 0; i < cnt.size(); i++) {
                self = (self->mp[cnt[i] - 'a']);
            }
            out << self->signs << endl;
        }
        if(op == 3) {
            node *self = &root;
            int len = 0;
            for(; len < cnt.size() && (self->mp[cnt[len]])->exists; len++) {
                node *next = self->mp[cnt[len]-'a'];
                if(next->exists) {
                    self = next;
                }
            }
            out << len << endl;
        }
    }

    return 0;
}