Pagini recente » Cod sursa (job #1030221) | Cod sursa (job #2620099) | Cod sursa (job #278134) | Rating Ellie White (bum_bum) | Cod sursa (job #1314193)
#include <cstdio>
#include <cstring>
#include <unordered_map>
FILE* in=fopen("trie.in","r");
FILE* out=fopen("trie.out","w");
//FILE* ver=fopen("trie.ok","r");
const int Q=25,INF=-200000;
char v[Q];
struct nod
{
std::unordered_map<char,nod*> fii;
// nod *fii[26];
int term;
int part;
nod()
{
term=0;
part=0;
// memset(fii,0,26);
}
};
int ask2(nod *p, char v[])
{
if(p==NULL)
{
return -1;
}
if(v[0]==0 || v[0]=='\n')
{
return 0;
}
return 1+ask2(p->fii[v[0]-'a'],v+1);
}
int ask1(nod *p, char v[])
{
if(p==NULL)
return 0;
if(v[0]==0 || v[0]=='\n')
{
return p->term;
}
return ask1(p->fii[v[0]-'a'],v+1);
}
nod *del(nod *p, char v[])
{
if(v[0]==0 || v[0]=='\n')
{
p->term--;
if(p->term==0 && p->part==0)
{
delete p;
p=NULL;
}
return p;
}
p->part--;
p->fii[v[0]-'a']=del(p->fii[v[0]-'a'],v+1);
if(p->term==0 && p->part==0)
{
delete p;
p=NULL;
}
return p;
return p;
}
nod *add(nod *p,char v[])
{
if(p==NULL)
p=new nod;
if(v[0]==0 || v[0]=='\n')
{
p->term++;
return p;
}
p->part++;
p->fii[v[0]-'a']=add(p->fii[v[0]-'a'],v+1);
return p;
}
int main()
{
int x;
nod *start=new nod;
int act,aux;
int nr=0;
while(fscanf(in,"%d ",&x)==1)
{
// nr++;
fgets(v,Q,in);
//if(nr==58038)
//{
// nr=0;
//}
if(x==0)
{
add(start,v);
}
if(x==1)
{
del(start,v);
// if(start==NULL)
// start=new nod;
}
if(x==2)
{
act=ask1(start,v);
// fscanf(ver,"%d",&aux);
//if(act!=aux)
//{
// act=ask1(start,v);
//}
fprintf(out,"%d\n",act);
}
if(x==3)
{
act=ask2(start,v);
// fscanf(ver,"%d",&aux);
// if(act!=aux)
// {
// act=ask2(start,v);
// }
fprintf(out,"%d\n",act);
}
}
return 0;
}