Pagini recente » Cod sursa (job #1693606) | Cod sursa (job #3270770) | Cod sursa (job #3276328) | Cod sursa (job #2679859) | Cod sursa (job #1951503)
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct nod
{
int v;
nod* nxt[26];
nod()
{
v=0;
memset(nxt, 0, sizeof(nxt));
}
};
int main()
{
int op; char cuv[25];
nod *r=new nod;
while(fin>>op>>cuv)
{
if(op==0)
{
nod* x=r;
for(int i=0;i<strlen(cuv);++i)
{
int c=cuv[i]-'a';
if(x->nxt[c]==0)
x->nxt[c]=new nod, x->nxt[c]->v++;
else
x->nxt[c]->v++;
x=x->nxt[c];
}
}
else if(op==1)
{
nod* x=r;
for(int i=0;i<strlen(cuv);++i)
{
int c=cuv[i]-'a';
if(x->nxt[c]!=0)
x->nxt[c]->v--;
x=x->nxt[c];
}
}
else if(op==2)
{
nod* x=r;
for(int i=0;i<strlen(cuv);++i)
{
int c=cuv[i]-'a';
x=x->nxt[c];
}
bool fin=true;
for(int i=0;i<26;++i)
if(x->nxt[i]!=0&&x->nxt[i]->v>0)fin=false;
fout<<(fin?(x->v):0)<<'\n';
}
else if(op==3)
{
int l=0;
nod* x=r;
for(int i=0;i<strlen(cuv);++i)
{
int c=cuv[i]-'a';
if(x->nxt[c]!=0 && x->nxt[c]->v>0)
l++, x=x->nxt[c];
else break;
}
fout<<l<<'\n';
}
}
return 0;
}