Pagini recente » Cod sursa (job #1024117) | Cod sursa (job #2547992) | Cod sursa (job #1141963) | Cod sursa (job #2088819) | Cod sursa (job #2984886)
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int NMAX=755;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct trie{
struct node{
int cnt=0,cnt2=0;
node* children[26]{};
}*root=new node;
void insert(const string& s, int cnt=1){
node* curr=root;
root->cnt+=cnt;
for(auto it : s){
if(curr->children[it-'a']==NULL) curr->children[it-'a']=new node;
curr=curr->children[it-'a'];
curr->cnt+=cnt;
}
curr->cnt2+=cnt;
}
void erase(const string& s){
insert(s,-1);
}
int count(const string& s){
node* curr=root;
for(auto it : s){
if(curr->cnt==0 || curr->children[it-'a']==NULL) return 0;
curr=curr->children[it-'a'];
}
return curr->cnt2;
}
int lcp(const string& s){
node* curr=root;
int depth=0;
for(auto it : s){
if(curr->children[it-'a']==NULL || curr->children[it-'a']->cnt==0) return depth;
curr=curr->children[it-'a'],depth++;
}
return depth;
}
}t;
int main()
{
ios_base::sync_with_stdio(false); fout.tie(0);
int type;
string s;
while(fin>>type>>s){
switch(type){
case 0: t.insert(s); break;
case 1: t.erase(s); break;
case 2: fout<<t.count(s)<<'\n'; break;
case 3: fout<<t.lcp(s)<<'\n'; break;
}
}
return 0;
}