Cod sursa(job #2768893)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 12 august 2021 15:47:55
Problema Trie Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define C (*s - 'a')
typedef struct O {
	int c,n;
	struct O *f[26];
}T;
T *t;
char l[36];
void I(T *o,char *s)
{
	if(*s=='\n') {
		o->c++;
        return;
	}
	if(!o->f[C])
		o->f[C]=(T*)malloc(sizeof(T)),
		o->f[C]->c=o->f[C]->n=0,memset(o->f[C]->f,0,sizeof(o->f[C]->f)),
		o->n++;
	I(o->f[C],s+1);
}
int D(T *o,char *s)
{
	if(*s=='\n')
		o->c--;
	else if(D(o->f[C],s+1))
        o->f[C]=0,o->n--;
	if(o->c==0&&o->n==0&&o!=t) {
		free(o);
        return 1;
	}
	return 0;
}
int Q(T *o,char *s)
{
	if(*s=='\n')
        return o->c;
	if(o->f[C])
		return Q(o->f[C],s+1);
	return 0;
}
int P(T *o,char *s,int k)
{
	if(*s=='\n'||!o->f[C])
		return k;
	return P(o->f[C],s+1,k+1);
}
int main()
{
	freopen("trie.in","r",stdin),freopen("trie.out","w",stdout),fgets(l,32,stdin),
	t=(T*)malloc(sizeof(T)),t->c=t->n=0,memset(t->f,0,sizeof(t->f));
	while(!feof(stdin)) {
        if(l[0]=='0')
            I(t,l+2);
        else if(l[0]=='1')
            D(t,l+2);
        else if(l[0]=='2')
            printf("%d\n",Q(t,l+2));
        else if(l[0]=='3')
            printf("%d\n",P(t,l+2,0));
		fgets(l,32,stdin);
	}
	return 0;
}