Cod sursa(job #1560224)

Utilizator PatrunjeluMarginean Bogdan Alexandru Patrunjelu Data 1 ianuarie 2016 23:49:09
Problema Trie Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 3.25 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);
}

void AddWordOccurrence(TrieNode& node, const std::string& word)
{
	if (word.empty())
	{
		node.OccurrenceCount++;
	}
	else
	{
		char currentChar = word[0];
		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.substr(1));
		}
		else
		{
			AddWordOccurrence(*existingChild->second, word.substr(1));
		}
	}
}

bool InternalDelete(TrieNode& node, const std::string& word)
{
	if (word.empty())
	{
		node.OccurrenceCount--;
	}
	else
	{
		const char currentChar = word[0];
		auto child = node.Children[currentChar];
		if (InternalDelete(*child, word.substr(1)))
		{
			node.Children.erase(currentChar);
			node.ChildCount--;
		}
	}
	bool nodeIsDeletable = node.OccurrenceCount <= 0 && node.ChildCount <= 0;
	return nodeIsDeletable;
}

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

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

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

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

void Execute(const InstructionWordPair& instructionAndWord, std::ostream& out)
{
	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') : 
		out << CountOccurrences(*root, word) << std::endl;
		break;
	case('3') :
		out << CountLongestCommonPrefix(*root, word) << std::endl;
		break;
	default: 
		break;
	}
}

int main()
{
	std::ifstream in("trie.in");
	std::ofstream out("trie.out");
	std::string line;
	while (std::getline(in, line))
	{
		auto instructionAndWord = LineToInstructionAndWord(line);
		Execute(instructionAndWord, out);
	}
	out.close();
	in.close();
}