Pagini recente » Cod sursa (job #2806124) | Cod sursa (job #1990761) | Cod sursa (job #2837747) | Cod sursa (job #2335188) | Cod sursa (job #1560230)
#include <fstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <memory>
struct TrieNode
{
int OccurrenceCount = 0;
int ChildCount = 0;
std::unordered_map<char, std::shared_ptr<TrieNode>> Children;
};
std::unique_ptr<TrieNode> root(new TrieNode());
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(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& line)
{
detail::AddWordOccurrence(node, line, 2);
}
void DeleteWordOccurrence(TrieNode& node, const std::string& line)
{
detail::Delete(node, line, 2);
}
int CountOccurrences(TrieNode& node, const std::string& line)
{
return detail::CountOccurrences(node, line, 2);
}
int CountLongestCommonPrefix(const TrieNode& node, const std::string& line)
{
return detail::CountLongestCommonPrefix(node, line, 2, 0);
}
void Execute(const std::string& line, std::ostream& out)
{
const char instruction = line[0];
switch (instruction)
{
case('0') :
AddWordOccurrence(*root, line);
break;
case('1') :
DeleteWordOccurrence(*root, line);
break;
case('2') :
out << CountOccurrences(*root, line) << std::endl;
break;
case('3') :
out << CountLongestCommonPrefix(*root, line) << 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))
{
Execute(line, out);
}
out.close();
in.close();
}