Cod sursa(job #2030624)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 1 octombrie 2017 21:11:09
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.83 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;
 
struct data
{
    int x,nxt[26],fin;
}tpl;
 
vector<data>vec;
int op,idx;
string s;
char ch = (char)('a' + 26);
 
void add(string s)
{
    int curr = 0;
    s += ch;
    for(auto it : s)
    {
        if(it == ch) {
            vec[curr].fin++;
            break;
        }
        if(!vec[curr].nxt[it - 'a']) {
            vec[curr].nxt[it - 'a'] = ++idx;
            vec.pb(tpl);
        }
        curr = vec[curr].nxt[it - 'a'];
        vec[curr].x++;
    }
}
 
void del(string s)
{
    int curr = 0;
    s += ch;
    for(auto it : s)
    {
        if(it == ch){
            vec[curr].fin--;
            return ;
        }
        curr = vec[curr].nxt[it - 'a'];
        vec[curr].x--;
    }
}
 
int cnt(string s)
{
    int curr = 0;
    s += ch;
    for(auto it : s)
    {
        if(it == ch) return vec[curr].fin;
        curr = vec[curr].nxt[it - 'a'];
        if(!curr) return 0;
    }
}
 
int mxprf(string s)
{
    int curr = 0,ans = 0;
    for(auto it : s)
    {
        curr = vec[curr].nxt[it - 'a'];
        if(!curr || vec[curr].x == 0) return ans;
        ans++;
    }
    return ans;
}
 
int32_t main(){_
    //freopen("input","r",stdin);
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    tpl = {};
    vec.pb(tpl);
    while(cin >> op >> s)
    {
        if(op == 0) add(s);
        if(op == 1) del(s);
        if(op == 2) cout << cnt(s) << '\n';
        if(op == 3) cout << mxprf(s) << '\n';
    }
}