Pagini recente » Cod sursa (job #82311) | Cod sursa (job #1779319) | Cod sursa (job #565708) | Cod sursa (job #698752) | Cod sursa (job #2239765)
#include <iostream>
#include <fstream>
#include <cstring>
#define CH *ch-'a'
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
int i,j,n;
char s[32],*p;
struct trie
{
int nrf,cnt;
trie *fii[26],*t;
trie ()
{
nrf=cnt=0;
memset(fii, 0, sizeof(fii));
}
};
trie *T = new trie;
void ins(trie *nd,char *ch)
{
if(*ch=='\0')
{ nd->cnt++; return; }
nd->fii[CH] = new trie;
nd->fii[CH]->t=nd;
nd->nrf++;
ins(nd->fii[CH], ch+1);
}
void del(trie *nd,char *ch)
{
if(nd->nrf==0 && nd->cnt==0)
{
trie *g = nd;
nd->t->fii[CH]=0;
nd->t->nrf--;
delete nd;
del(g->t, ch-1);
}
}
void prefx(trie *nd,char *ch)
{
if(*ch=='\0' || nd->fii[CH]==0)
{
if(s[0]=='0') ins(nd, ch);
if(s[0]=='1') nd->cnt--, del(nd, ch-1);
if(s[0]=='2')
{
if(*ch=='\0') fout<<nd->cnt<<"\n";
else fout<<0<<"\n";
}
if(s[0]=='3') fout<<ch-s-2<<"\n";
return;
}
prefx(nd->fii[CH], ch+1);
}
int main() {
while(fin.getline(s, 32))
prefx(T, s+2);
}