Pagini recente » Cod sursa (job #2477) | Cod sursa (job #2713289) | Cod sursa (job #2857018) | Cod sursa (job #335653) | Cod sursa (job #928963)
Cod sursa(job #928963)
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int lg;
char sir[25];
struct nod {
int v[26],wd,pfx;
} blank;
vector<nod> T;
void add (int nod, int crt)
{
if (crt==lg)
T[nod].wd++;
else
{
int next_nod=T[nod].v[sir[crt]-'a'];
if (!next_nod)
next_nod=T.size(), T.push_back(blank);
T[nod].v[sir[crt]-'a']=next_nod;
add(next_nod,crt+1);
}
T[nod].pfx++;
}
void erase (int nod, int crt)
{
if (crt==lg)
T[nod].wd--;
else
erase(T[nod].v[sir[crt]-'a'],crt+1);
T[nod].pfx--;
}
int query_words (int nod, int crt)
{
if (crt==lg)
return T[nod].wd;
else
{
int next_nod=T[nod].v[sir[crt]-'a'];
if (!next_nod)
return 0;
return query_words(next_nod,crt+1);
}
}
int query_lcp (int nod, int crt)
{
if (crt==lg)
return crt;
else
{
int next_nod=T[nod].v[sir[crt]-'a'];
if (!next_nod || !T[next_nod].pfx)
return crt;
return query_lcp(next_nod,crt+1);
}
}
int main ()
{
int tip=0;
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
T.push_back(blank);
T.push_back(blank);
T[0].pfx=T[1].pfx=1;
while (tip!=-1)
{
tip=-1;
scanf("%d %s",&tip,sir);
lg=strlen(sir);
if (tip==0)
add(1,0);
if (tip==1)
erase(1,0);
if (tip==2)
printf("%d\n",query_words(1,0));
if (tip==3)
printf("%d\n",query_lcp(1,0));
}
return 0;
}