Cod sursa(job #1560534)

Utilizator PatrunjeluMarginean Bogdan Alexandru Patrunjelu Data 2 ianuarie 2016 20:12:26
Problema Trie Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 3.86 kb
#include <fstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <memory>

using InstructionWordPair = std::tuple<char, std::string>;

struct TrieNode
{
	int OccurrenceCount = 0;
	int ChildCount = 0;
	std::unordered_map<char, std::shared_ptr<TrieNode>> Children;
};

std::unique_ptr<TrieNode> root(new TrieNode());

InstructionWordPair LineToInstructionAndWord(const std::string& line)
{
	char instruction = line[0];
	std::string word = line.substr(2);
	return std::make_tuple(instruction, word);
}

namespace detail
{
	bool Delete(TrieNode& node, const std::string& word, int indexInWord)
	{
		if (indexInWord == word.length())
		{
			node.OccurrenceCount--;
		}
		else
		{
			const char currentChar = word[indexInWord];
			auto child = node.Children[currentChar];
			if (Delete(*child, word, indexInWord + 1))
			{
				node.Children.erase(currentChar);
				node.ChildCount--;
			}
		}
		bool nodeIsDeletable = node.OccurrenceCount <= 0 && node.ChildCount <= 0;
		return nodeIsDeletable;
	}

	int CountLongestCommonPrefix(const TrieNode& node, const std::string& word, int indexInWord, int lengthSoFar)
	{
		if (indexInWord == word.length())
		{
			return lengthSoFar;
		}
		const char currentChar = word[indexInWord];
		const auto child = node.Children.find(currentChar);
		if (child != node.Children.end())
		{
			return CountLongestCommonPrefix(*child->second, word, indexInWord + 1, lengthSoFar + 1);
		}
		return lengthSoFar;
	}

	void AddWordOccurrence(TrieNode& node, const std::string& word, int indexInWord)
	{
		if (indexInWord == word.length())
		{
			node.OccurrenceCount++;
		}
		else
		{
			char currentChar = word[indexInWord];
			auto existingChild = node.Children.find(currentChar);
			if (existingChild == node.Children.end())
			{
				auto newNode = std::make_shared<TrieNode>();
				node.Children[currentChar] = newNode;
				node.ChildCount++;
				AddWordOccurrence(*newNode, word, indexInWord + 1);
			}
			else
			{
				AddWordOccurrence(*existingChild->second, word, indexInWord + 1);
			}
		}
	}

	int CountOccurrences(const TrieNode& node, const std::string& word, int indexInWord)
	{
		if (indexInWord == word.length())
		{
			return node.OccurrenceCount;
		}
		const char currentChar = word[indexInWord];
		const auto child = node.Children.find(currentChar);
		if (child != node.Children.end())
		{
			return CountOccurrences(*child->second, word, indexInWord + 1);
		}
		return 0;
	}
}

void AddWordOccurrence(TrieNode& node, const std::string& word)
{
	detail::AddWordOccurrence(node, word, 0);
}

void DeleteWordOccurrence(TrieNode& node, const std::string& word)
{
	detail::Delete(node, word, 0);
}

int CountOccurrences(const TrieNode& node, const std::string& word)
{
	return detail::CountOccurrences(node, word, 0);
}

int CountLongestCommonPrefix(const TrieNode& node, const std::string& word)
{
	return detail::CountLongestCommonPrefix(node, word, 0, 0);
}

void Execute(const InstructionWordPair& instructionAndWord)
{
	const char instruction = std::get<0>(instructionAndWord);
	const std::string word = std::get<1>(instructionAndWord);
	switch (instruction)
	{
	case('0') :
		AddWordOccurrence(*root, word);
		break;
	case('1') :
		DeleteWordOccurrence(*root, word);
		break;
	case('2') :
		printf("%d\n", CountOccurrences(*root, word));
		break;
	case('3') :
		printf("%d\n", CountLongestCommonPrefix(*root, word));
		break;
	default:
		break;
	}
}

int main()
{
	char line[32];
	freopen("trie.in", "r", stdin);
	freopen("trie.out", "w", stdout);
	fgets(line, 32, stdin);
	while (!feof(stdin))
	{
		char instruction = line[0];
		int lineLen = 0;
		do
		{
			lineLen++;
		} while (line[lineLen] != '\n');
		std::string word(line + 2, line + lineLen);
		Execute(std::make_tuple(instruction, word));
		fgets(line, 32, stdin);
	}
}