Cod sursa(job #302581)

Utilizator hurrycaneBogdan Gaza hurrycane Data 9 aprilie 2009 00:35:43
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<cstring>
#include<cstdio>
#define CH (*s-'a')

struct trie{
	int c;
	int nrfii;
	trie *fiu[30];
	trie(){
		c=0;
		nrfii=0;
		memset(fiu,0,sizeof(fiu));
	}
};

trie *A=new trie;

void ins(trie *tr,char *s){
	if(*s=='\n'){
		tr->c++;
		return ;
	}

	if(tr->fiu[CH]==0){
		tr->fiu[CH]=new trie;
		tr->nrfii++;
	}
	ins(tr->fiu[CH],s+1);
}

int dlt(trie *tr,char *s){
	if(*s=='\n'){
		tr->c--;
	}else if(dlt(tr->fiu[CH],s+1)){
		tr->fiu[CH]=0;
		tr->nrfii--;
	}

	if(tr->c==0&&tr->nrfii==0&&tr!=A){
		delete tr; return 1;
	}
	return 0;
}

int src(trie *tr,char *s){
	if(*s=='\n'){
		return tr->c;
	}

	if(tr->fiu[CH])
		return src(tr,s+1);
	return 0;
}

int fix(trie *tr,char *s,int lung){
	if(*s=='\n'||tr->fiu[CH]==0){
		return lung;
	}
	return fix(tr->fiu[CH],s+1,lung+1);
}

int main(){
	char read[35];
	freopen("trie.in","r",stdin);
	freopen("trie.out","w",stdout);

	fgets(read,35,stdin);
	while(!feof(stdin)){
		if(read[0]=='0') ins(A,read+2);
		if(read[0]=='1') dlt(A,read+2);
		if(read[0]=='2') printf("%d\n",src(A,read+2));
		if(read[0]=='3') printf("%d\n",fix(A,read+2,0));

		fgets(read,35,stdin);
	}
	return 0;
}