Cod sursa(job #3199607)

Utilizator rares89_Dumitriu Rares rares89_ Data 1 februarie 2024 22:37:29
Problema Trie Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <unordered_map>

using namespace std;

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

unordered_map<string, int> mp, pref;

int main() {
    int c;
    string w;
    while(fin >> c >> w) {
        switch(c) {
            case 0:
                mp[w]++;
                for(; w != ""; w.pop_back()) {
                    pref[w]++;
                }
                break;
            case 1:
                mp[w]--;
                for(; w != ""; w.pop_back()) {
                    pref[w]--;
                }
                break;
            case 2:
                fout << mp[w] << "\n";
                break;
            default:
                bool gasit = false;
                for(; w != "" && !gasit; w.pop_back()) {
                    if(pref[w] > 0) {
                        fout << w.size() << "\n";
                        gasit = true;
                    }
                }
                if(!gasit) {
                    fout << "0\n";
                }
                break;
        }
    }
    return 0;
}