Cod sursa(job #2482398)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 28 octombrie 2019 10:58:38
Problema Trie Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>
#include <map>

using namespace std;

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

struct node {
    map< char, node > mp;
    int signs = 0, exists = 0;
};

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

    node root;

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

    return 0;
}