Cod sursa(job #2627751)

Utilizator RobertLearnsCDragomir Robert. RobertLearnsC Data 12 iunie 2020 13:06:19
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
/*
 * Citire prin C++ si afisare  prin C, fain nu? :)))))))
 */
#include <bits/stdc++.h>
using namespace std;

unordered_map<string, int> words_count, trie;
string word;

void insert(string word) {
    words_count[word]++;
    while(word.size()) {
        trie[word]++;
        word.pop_back();
    }
}

void delete_ap(string word) {
    if(!words_count[word]) return;

    words_count[word]--;
    while(word.size()) {
        trie[word]--;
        word.pop_back();
    }
}

void print_ap(string word) {
    printf("%d\n",words_count[word]);
}

void print_longest_prefix(string word) {
    while(word.size()) {
       if(trie[word]) {
           printf("%d\n",word.size());
           break;
       }
       word.pop_back();
    }
}

int main() {

    ifstream in("trie.in");
    freopen("trie.out", "w", stdout);

    while(getline(in, word)) {
        switch(word[0]) {
            case '0':
                insert(&word[2]);
                break;
            case '1':
                delete_ap(&word[2]);
                break;
            case '2':
                print_ap(&word[2]);
                break;
            case '3':
                print_longest_prefix(&word[2]);
                break;
        }
    }
    return 0;
}