Cod sursa(job #831375)

Utilizator axnsanCristi Vijdea axnsan Data 8 decembrie 2012 16:00:51
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.04 kb
//#include <iostream>
#include <fstream>
#include <cstring>
//#include <ctime>
using namespace std;
const unsigned MAX_LEN = 20U;
const unsigned SIGMA = 26U;
class Trie
{
public:
	Trie() : Root(new Node) { ++(Root->Count); }
	void Add(const char* w);
	void Delete(const char* w);
	unsigned Count(const char* w);
	unsigned LongestPrefixOf(const char* w);

private:
	struct Node {
		Node *Children[SIGMA];
		unsigned Count;
		unsigned char numChildren;
		Node() : Count(0), numChildren(0) { memset(Children, 0, sizeof Children); }
		inline void AddChild(unsigned c) { if (Children[c] == NULL) { Children[c] = new Node; ++numChildren; } }
		inline void RemoveChild(unsigned c) { if (Children[c] != NULL) { delete Children[c]; Children[c] = NULL; --numChildren; } }
		/*~Node() { 
			for (unsigned i = 0;i < SIGMA;++i)
				delete Children[i];
		}*/
	} *const Root;
};
int main()
{
	ifstream f("trie.in");
	ofstream g("trie.out");
	char w[MAX_LEN+3];
	Trie T;
	while (!f.getline(w, 23).eof())
	{
		switch (*w)
		{
		case '0':
			T.Add(w+2);
			break;
		case '1':
			T.Delete(w+2);
			break;
		case '2':
			g << T.Count(w+2) << '\n';
			break;
		default:
			g << T.LongestPrefixOf(w+2) << '\n';
			break;
		}
	}

	f.close();
	g.close();
	return 0;
}

void Trie::Add(const char* w)
{
	Node* n = Root;
	while (*w)
	{
		n->AddChild(*w-'a');
		n = n->Children[*w++ -'a'];
	}
	++(n->Count);
}
unsigned Trie::Count(const char* w)
{
	Node* n = Root;
	while (*w && n != NULL)
		n = n->Children[*w++ -'a'];

	if (n != NULL)
		return n->Count;
	
	return 0;
}
void Trie::Delete(const char* w)
{
	Node* n[MAX_LEN];
	unsigned i = 0;
	n[0] = Root;
	while (*w)
	{
		++i;
		n[i] = n[i-1]->Children[*w++ -'a'];
	}

	--(n[i]->Count);
	while (n[i]->Count == 0 && n[i]->numChildren == 0)
		n[--i]->RemoveChild(*(--w)-'a');
}
unsigned Trie::LongestPrefixOf(const char* w)
{
	Node* n = Root;
	const char *p = w;
	while (*p && n != NULL)
		n = n->Children[*p++ -'a'];

	if (n != NULL)
		return p-w;

	return p-w-1;
}