Cod sursa(job #2731636)

Utilizator om6gaLungu Adrian om6ga Data 28 martie 2021 00:00:54
Problema Trie Scor 100
Compilator c-32 Status done
Runda Arhiva educationala Marime 4.64 kb
#include <stdio.h>
#include <stdlib.h>
 
#define CH (*s - 'a')

struct TrieNode {
	struct TrieNode *parent;
	struct TrieNode *child[26];
	char childrenCount; // how many children it has - presumably useful for deletion
	short int freq; // nr of appearances of the word formed by the characters belonging to the path starting at root and ending here
};
typedef struct TrieNode TrieNode;

TrieNode mem[115000]; // chunk of 120000 nodes
int alloc_cnt = 1;
TrieNode *T = mem;  // first node in chunk we'll be using as root
int dbg_flag;
int ch, isnull;

/* 0 w - adauga o aparitie a cuvantului w in lista
1 w - sterge o aparitie a cuvantului w din lista
2 w - tipareste numarul de aparitii ale cuvantului w in lista
3 w - tipareste lungimea celui mai lung prefix comun al lui w cu oricare cuvant din lista

Pentru toate operatiile, cuvantul w este format din maxim 20 de caractere din intervalul 'a'..'z'
Numarul total de operatii nu va depasi 100 000
Operatiile de tip 1 w vor aparea numai daca w apare cel putin o data in lista de cuvinte */


int rm_rec(TrieNode *node, char *s) {
	if  (s[0] < 'a') {
        node->freq--;
	}
	else if (rm_rec(node->child[CH], s+1)) {
		node->child[CH] = 0;
		node->childrenCount--;
	}
	if (node->freq == 0 && node->childrenCount == 0 && node != T) {
		//free(node);
		return 1;
	}
	return 0;
}

void add(TrieNode *node, char *s) {  // assume word '\0' terminated
    //int isnull;
	while (1) {
		ch = *s - 'a';
		if (s[0] == 0) { // have hit either '\0' or '\n'
			node->freq++;
		    return;
		}
		isnull = (node->child[ch] == NULL);
		node->childrenCount += isnull;
		node->child[ch] = (isnull ? mem + alloc_cnt++ : node->child[ch]);
		//node->child[ch]->parent = (isnull ? node : node->child[ch]->parent);
		node = node->child[ch];
		
		/*if (node->child[ch] == NULL) {
			node->childrenCount++;
			node->child[ch] = mem + alloc_cnt++;
			node->child[ch]->parent = node;
		}
		node = node->child[ch];*/
		s++;
	}
}
/*
int rm(TrieNode *node, char *s) {
	while (1) {
		if (s[0] == 0) { // have hit either '\0' or '\n'
		    node = ((--node->freq == 0 && node->childrenCount == 0) ? node->parent : NULL);
		    break;
		}
		node = node->child[CH];
		s++;
	}
	s--;
	while (node != NULL) {
		ch = *s - 'a';
		node->child[CH] = 0;
		node->childrenCount--;
		node = ((node->freq == 0 && node->childrenCount == 0) ? node->parent : NULL);
		s--;
	}
}*/




void rm(TrieNode *node, char *s) {
	for (;;) {
		if (s[0] == 0) { // have hit either '\0' or '\n'
		    node->freq--;
		    node = ((node->freq == 0 && node->childrenCount == 0) ? node->parent : NULL);
		    if (node == NULL) // no need to fix up the parent chain
	            return;
		    s--;
		    break;
		}
		node = node->child[CH];
		s++;
	}
	for (;;) {
		node->child[CH] = NULL;
		node->childrenCount--;
		node = ((node->freq == 0 && node->childrenCount == 0) ? node->parent : NULL);
		if (node == NULL)
		    return;
		s--;
	}
}

int query(TrieNode *node, char *s) {
	while (1) {
		if (s[0] == 0)
		    return node->freq;
		node = node->child[CH];
	    if (node == NULL)
	        return 0;
	    s++;
	}
	return 0;
}

int prefix(TrieNode *node, char *s) {	
    int t = 0;
    
	while (1) {
		node = node->child[CH];
		if (node == NULL || s[0] == 0)
		    return t;
		s++;
		t++;
	}
}

void inorder(TrieNode *node, char *buf, int k) {
	int i;
	
	if (node->freq > 0) {
		buf[k] = '\0';
		printf("%s\n", buf);
	}
	for (i = 0; i < 26; i++)
	    if (node->child[i]) {
	    	buf[k] = i+'a';
	    	inorder(node->child[i], buf, k+1);
		}
}

int tot_add, tot_rm, tot_q, tot_p;
char output[200000];
int oindex;
char input[2000000];
int iindex;

int main() {
	char line[32], buf[32];
	char *s;
	int i = 0, op;
 
    freopen( "trie.in", "r", stdin );
	freopen( "trie.out", "w", stdout );
	//freopen( "grader_test20.in", "r", stdin );
	//freopen( "grader_test20.out", "w", stdout );
	//fgets(line, 32, stdin);
	
	iindex = read(0, input, 2000000);
	//write(1, input, 150000);

	while (i < iindex) {
		op = input[i];
		s = &input[i+2];
		while (input[i] != '\n')
		    i++;
		input[i++] = 0;
		switch(op) {
			case '0':
				add(T, s);
			    break;
			case '1':
				rm_rec(T, s);
			    break;
			case '2':
				oindex += sprintf(output+oindex, "%d\n", query(T, s));
			    break;
			case '3':
				oindex += sprintf(output+oindex, "%d\n", prefix(T, s));
			    break;
		}
	}
	write(1, output, oindex);
	//printf("tot_add = %d, tot_rm = %d, tot_q = %d, tot_p = %d\n", tot_add, tot_rm, tot_q, tot_p);
	//inorder(T, buf, 0);
	//printf("alloc_cnt = %d\n", alloc_cnt);
	return 0;
}