Pagini recente » Cod sursa (job #2948236) | Cod sursa (job #2416644) | Cod sursa (job #2252130) | Cod sursa (job #1360354) | Cod sursa (job #3209182)
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'009
#define dim 100005
#define lim 1000000
#define INF 1000000000
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define piii pair<int,pair<int,int> >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct trie
{
struct nod
{
int cnt;
int nr;
nod *next[26];
nod()
{
nr=0;
cnt=0;
for(int i=0; i<26; i++)
next[i]=nullptr;
}
};
nod *rad=new nod;
void insert(const string &a)
{
nod *aux=rad;
for(int i=0; i<(int)a.size(); i++)
{
if(aux->next[a[i]-'a']==nullptr)
{
aux->next[a[i]-'a']=new nod;
aux=aux->next[a[i]-'a'];
aux->cnt++;
}
else
{
aux=aux->next[a[i]-'a'];
aux->cnt++;
}
}
aux->nr++;
}
int count(const string &a)
{
nod *aux=rad;
for(int i=0; i<(int)a.size(); i++)
{
if(aux->next[a[i]-'a']==nullptr)
return 0;
else
{
aux=aux->next[a[i]-'a'];
}
}
return aux->nr;
}
int lcp(const string &a)
{
nod *aux=rad;
int ans=0;
for(int i=0; i<(int)a.size(); i++)
{
if(aux->next[a[i]-'a']==nullptr || aux->next[a[i]-'a']->cnt==0)
return ans;
else
{
ans++;
aux=aux->next[a[i]-'a'];
}
}
return ans;
}
void erase(const string &a)
{
nod *aux=rad;
for(int i=0; i<(int)a.size(); i++)
{
aux=aux->next[a[i]-'a'];
aux->cnt--;
}
aux->nr--;
}
}v;
int tip;
string s;
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(nullptr);
fout.tie(nullptr);
while(fin >> tip >> s)
{
if(tip==0)
{
v.insert(s);
}
else if(tip==1)
{
v.erase(s);
}
else if(tip==2)
{
fout << v.count(s) << "\n";
}
else
{
fout << v.lcp(s) << "\n";
}
}
return 0;
}