Cod sursa(job #2029315)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 29 septembrie 2017 20:38:42
Problema Trie Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
# pragma GCC optimize("O3")
# include <bits/stdc++.h>
# define maxn 100005
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define sz(x) (int)((x).size())
//# define int ll
using namespace std;
 
map<int,int>t;;
int op;
string s;
map<pair<int,int>,int>nxt;
int idx;
 
void add(string s)
{
    int curr = 0;
    s += '{';
    for(auto it : s)
    {
        if(!nxt[mp(curr,it - 'a')]) nxt[mp(curr,it - 'a')] = ++idx;
        curr = nxt[mp(curr,it - 'a')];
        t[curr]++;
    }
}
 
void del(string s)
{
    int curr = 0;
    s += '{';
    for(auto it : s)
    {
        curr = nxt[mp(curr,it - 'a')];
        t[curr]--;
    }
}
 
int cnt(string s)
{
    int curr = 0;
    s += '{';
    for(auto it : s)
    {
        curr = nxt[mp(curr,it - 'a')];
        if(!curr || !t[curr]) return 0;
    }
    return t[curr];
}
 
int mxprf(string s)
{
    int curr = 0;
    int ans = 0;
    for(auto it : s)
    {
        curr = nxt[mp(curr,it - 'a')];
        if(!curr || !t[curr]) return ans;
        ans++;
    }
    return ans;
}
 
int32_t main(){_
    //freopen("input","r",stdin);
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    while(cin >> op >> s)
    {
        if(op == 0) add(s);
        else if(op == 1) del(s);
        else if(op == 2) cout << cnt(s) << '\n';
        else cout << mxprf(s) << '\n';
    }
}