Cod sursa(job #2239897)

Utilizator shantih1Alex S Hill shantih1 Data 11 septembrie 2018 23:12:33
Problema Trie Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#define CH *ch-'a'

using namespace std;

int rz;
bool ok;
char s[32],*p,op;

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

bool pot(trie *nd)
{
	return (nd->cnt==0 && nd->nrf==0 && nd!=T);
}
void merg (trie *nd,char *ch)
{
	if(*ch=='\n')
	{
		if(op=='0')	nd->cnt++;
		if(op=='2')	printf("%d\n",nd->cnt);
		if(op=='3')	printf("%d\n",rz);
		if(op=='1')
		{
			nd->cnt--;
			ok=pot(nd);
			if(ok)	delete nd;
		}
		return;
	}
	if(nd->fiu[CH]==0)
	{
		if(op=='3')
		{
			printf("%d\n",rz);
			return;
		}
		nd->fiu[CH] = new trie;
		nd->nrf++;
	}
	else rz++;
	merg(nd->fiu[CH], ch+1);
	
	if(ok)
	{
		nd->fiu[CH]=0;
		nd->nrf--;
		ok=pot(nd);
		if(ok)	delete nd;
	}
}

int main() {
	
	freopen("trie.in", "r", stdin);
	freopen("trie.out", "w", stdout);
	
	while(fgets(s, 32, stdin)){
		rz=0;
		op=s[0];
		merg(T, s+2);
	}
	return 0;
}