Pagini recente » Cod sursa (job #2163869) | Cod sursa (job #1190995) | Cod sursa (job #1666286) | Cod sursa (job #788588) | Cod sursa (job #1990791)
#include <fstream>
#include<cstring>
using namespace std;
ifstream cin ("trie.in");
ofstream cout ("trie.out");
struct bla
{
int nod,urm,start,see,nr; char c;
} t[2000001];
char s[30]; int p,nodes,l,y;
void adauga (int varf,int poz)
{ t[varf].see++;
if(poz>l) { t[varf].nr++; return;}
for(int x=t[varf].start;x!=0;x=t[x].urm)
{
if(t[x].c==s[poz]) {adauga(t[x].nod,poz+1); return;}
}
nodes++;
t[nodes].urm=t[varf].start; t[varf].start=nodes; t[nodes].c=s[poz]; t[nodes].nod=nodes;
adauga(nodes,poz+1);
}
void sterge (int varf,int poz)
{
t[varf].see--;
if(poz>l) {t[varf].nr--; return;}
for(int x=t[varf].start;x!=0;x=t[x].urm)
{
if(t[x].c==s[poz]) sterge(t[x].nod,poz+1);
}
}
void scrie (int varf,int poz)
{
if(poz>l) {cout<<t[varf].nr<<"\n"; y=1; return;}
for(int x=t[varf].start;x!=0;x=t[x].urm)
{
if(t[x].c==s[poz]) scrie(t[x].nod,poz+1);
}
}
void tipar (int varf,int poz)
{
if(poz>l || t[varf].see==0) {cout<<poz-1<<"\n"; return;}
for(int x=t[varf].start;x!=0;x=t[x].urm)
{
if(t[x].c==s[poz]) {tipar(t[x].nod,poz+1); return;}
}
cout<<poz<<"\n";
}
void read ()
{
while(cin>>p)
{
cin>>s;
l=strlen(s)-1;
if(p==0) adauga(0,0);
else if(p==1) sterge(0,0);
else if(p==2) {y=0,scrie(0,0); if(y==0) cout<<0<<"\n"; }
else if(p==3) tipar(0,0);
}
}
int main()
{
read();
cin.close();
cout.close();
return 0;
}