Cod sursa(job #3214577)

Utilizator Alex_DumitrascuAlex Dumitrascu Alex_Dumitrascu Data 14 martie 2024 11:11:33
Problema Trie Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

#define ll long long

using namespace std;

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

map <string, int> mp;

int longest_common_prefix(string word1, string word2)
{
    int siz=0, index=0;
    while (index<word1.size()&&index<word2.size()) {
        if (word1[index]==word2[index]) {
            siz++;
        }
        else {
            break;
        }
        index+=1;
    }
    return siz;
}

int main()
{
    fin.tie(0); fin.sync_with_stdio(false);
    int op; string word;
    while (fin>>op>>word) {
        if (op==0) {
            mp[word]+=1;
        }
        if (op==1) {
            mp[word]-=1;
            if (mp[word]==0) mp.erase(word);
        }
        if (op==2) {
            fout<<mp[word]<<'\n';
        }
        if (op==3) {
            auto it=mp.lower_bound(word);
            auto it1=it, it2=it; it1--; it2++;
            string word1=it1->first, word2=it2->first;
            int prefix1=longest_common_prefix(word, word1);
            int prefix2=longest_common_prefix(word, word2);
            fout<<max(prefix1, prefix2)<<'\n';
        }
    }
    return 0;
}