Pagini recente » Monitorul de evaluare | Cod sursa (job #2792767) | Cod sursa (job #1681986) | Cod sursa (job #462583) | Cod sursa (job #925997)
Cod sursa(job #925997)
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
#define lgmax 23
struct nod{ long v[27], nr, nctr;
void init()
{
for (char ii='a';ii!='z';ii++)
v[ii-'a']=0;
}
};
vector <nod> tr;
nod ng;
long i, n, op, ls, y;
char s[lgmax];
void adauga(long x, long poz)
{
if (poz==ls)
tr[x].nr++;
else
{
y=tr[x].v[s[poz]-'a'];
if (y==0)
{ y=tr.size(); tr.push_back(ng); }
tr[x].v[s[poz]-'a']=y;
adauga(y,poz+1);
}
tr[x].nctr++;
}
void sterge(long x, long poz)
{
if (poz==ls)
tr[x].nr--;
else
{
y=tr[x].v[s[poz]-'a'];
sterge(y,poz+1);
}
tr[x].nctr--;
}
void query1(long x, long poz)
{
if (poz==ls)
printf("%ld\n",tr[x].nr);
else
{
y=tr[x].v[s[poz]-'a'];
if (y==0)
printf("0\n");
else
query1(y,poz+1);
}
}
void query2(long x, long poz)
{
if (poz==ls)
printf("%ld\n",poz-1);
else
{
y=tr[x].v[s[poz]-'a'];
if ((y==0)||(tr[y].nctr==0))
printf("%ld\n",poz);
else
query2(y,poz+1);
}
}
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
tr.push_back(ng); tr.push_back(ng);
tr[0].nctr=tr[1].nctr=1;
while (1)
{
op=-1; scanf("%ld %s",&op,&s);
ls=strlen(s);
if (op==-1)
break;
if (op==0)
adauga(1,0);
if (op==1)
sterge(1,0);
if (op==2)
query1(1,0);
if (op==3)
query2(1,0);
}
return 0;
}