Cod sursa(job #2239713)

Utilizator shantih1Alex S Hill shantih1 Data 11 septembrie 2018 18:23:15
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#define CH *ch-'a'

using namespace std;

ifstream fin("trie.in");
ofstream fout("trie.out");

int i,j,n;
char s[30];

struct trie
{
	int nrf,cnt;
	trie *fii[26],*t;
	
	trie ()
	{
		nrf=cnt=0;
		memset(fii, 0, sizeof(fii));
	}
};
trie *T = new trie, *p;

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-1)-'a']=0;
		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);	//vreau sa sterg cuvantul;
		if(s[0]=='2')	fout<<nd->cnt<<"\n";
		if(s[0]=='3')	fout<<ch-s-2<<"\n";
		return;
	}
	prefx(nd->fii[CH], ch+1);
}

int main() {
	
	while(fin.getline(s, 200))
		prefx(T, s+2);
}