Cod sursa(job #2446814)

Utilizator miguelMihail Lavric miguel Data 10 august 2019 19:44:50
Problema Trie Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.52 kb
/*
░░░░░░░░░░░░░░░░▄▄█▀▀██▄▄░░░░░░░
░░░░░░░░░░░░░▄█▀▀░░░░░░░▀█░░░░░░
░░░░░░░░░░░▄▀░░░░░░░░░░░░░█░░░░░
░░░░░░░░░▄█░░░░░░░░░░░░░░░█░░░░░
░░░░░░░██▀░░░░░░░▄▄▄░░▄░█▄█▄░░░░
░░░░░▄▀░░░░░░░░░░████░█▄██░▀▄░░░
░░░░█▀░░░░░░░░▄▄██▀░░█████░██░░░
░░░█▀░░░░░░░░░▀█░▀█▀█▀▀▄██▄█▀░░░
░░░██░░░░░░░░░░█░░█░█░░▀▀▄█▀░░░░
░░░░█░░░░░█░░░▀█░░░░▄░░░░░▄█░░░░
░░░░▀█░░░░███▄░█░░░░░░▄▄▄▄█▀█▄░░
░░░░░▀██░░█▄▀▀██░░░░░░░░▄▄█░░▀▄░
░░░░░░▀▀█▄░▀▄▄░▄░░░░░░░███▀░░▄██
░░░░░░░░░▀▀▀███▀█▄░░░░░█▀░▀░░░▀█
░░░░░░░░░░░░▄▀░░░▀█▄░░░░░▄▄░░▄█▀
░░░▄▄▄▀▀▀▀▀█▀░░░░░█▄▀▄▄▄▄▄▄█▀▀░░
░▄█░░░▄██▀░░░░░░░░░█▄░░░░░░░░░░░
█▀▀░▄█░░░░░░░░░░░░░░▀▀█▄░░░░░░░░
█░░░█░░░░░░░░░░░░░░░░░░█▄░░░░░░░
*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair <int, int>
#define vi vector <int>
//#define =1e9 =(int)1e9
const ll mod = 1000000007;
const ll nmax=1000003;
//#define int ll
int t, idx;
string s;
char ch='a'+26;

struct nod{
    int cnt, fin, next[26];
}init;

vector<nod> v;

void add(string s){
    int cur=0;
    s=s+ch;
    for(char i: s){
        if(i==ch){
            v[cur].fin++;
            break;
        }
        else{
            if(v[cur].next[i-'a']==0){
                v[cur].next[i-'a']=++idx;
                v.pb(init);
            }
            cur=v[cur].next[i-'a'];
            v[cur].cnt++;
        }
    }
}

void del(string s){
    int cur=0;
    s=s+ch;
    for(char i: s){
        if(i==ch){
            v[cur].fin--;
            return;
        }
        cur=v[cur].next[i-'a'];
        v[cur].cnt--;
    }
}

int cont(string s){
    int cur=0;
    s=s+ch;
    for(char i : s){
        if(i==ch) return v[cur].fin;
        else cur=v[cur].next[i-'a'];
    }
}

int pref(string s){
    int cur=0, xd=0, ans=0;
    s=s+ch;
    for(char i: s){
        cur=v[cur].next[i-'a'];
        if(cur && v[cur].cnt>=1) xd++;
        else return xd;
    }
    return xd;
}


int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    ifstream cin("trie.in");
    ofstream cout("trie.out");
    v.pb(init);
    while(cin>>t>>s){
        if(t==0) add(s);
        else if(t==1) del(s);
        else if(t==2) cout<<cont(s)<<"\n";
        else if(t==3) cout<<pref(s)<<"\n";
    }
}