Cod sursa(job #2776222)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 18 septembrie 2021 22:01:14
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream F("trie.in");
ofstream G("trie.out");
#define C (*s - 'a')
struct T {
	int c,n;
	T *f[26];
	T() {
		c=n=0;
		memset(f,0,sizeof(f));
	}
};
T *t=new T;
char l[32];
void I(T *o,char *s)
{
	if(*s=='\n') {
		o->c++;
        return;
	}
	if(!o->f[C]) {
		o->f[C]=new T;
		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) {
		delete 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]==0)
		return k;
	return P(o->f[C],s+1,k+1);
}
int main()
{
	F.getline(l,32);
	while(!F.eof()) {
        if(l[0]=='0')
            I(t,l+2);
        else if(l[0]=='1')
            D(t,l+2);
        else if(l[0]=='2')
            G<<Q(t,l+2)<<"\n";
        else if(l[0]=='3')
            G<<P(t,l+2,0)<<"\n";
		F.getline(l,32);
	}
	return 0;
}