Pagini recente » Cod sursa (job #2553945) | Cod sursa (job #1345640) | Cod sursa (job #480974) | Cod sursa (job #1751635) | Cod sursa (job #1721689)
#ifndef _CRT_NONSTDC_NO_WARNINGS
#define _CRT_NONSTDC_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
class TrieNode {
private:
void destructTrie(TrieNode* root) {
for (auto item : root->child) {
auto key = item.first;
destructTrie((root->child)[key]);
delete((root->child)[key]);
(root->child)[key] = nullptr;
}
(root->child).erase(root->child.begin(), root->child.end());
}
bool del(TrieNode* curent, const string& word, const int& index) {
auto node = curent->child[word[index]];
if (index == (int)word.size()) {
curent->count--;
if (curent->count == 0) curent->child.erase(word[index]);
return (curent->count == 0 && curent->child.size() == 0);
}
bool shouldBeDeleted = del(node, word, index + 1);
if (shouldBeDeleted) {
curent->child.erase(word[index]);
delete(node);
node = nullptr;
return (curent->child.size() == 0);
}
return false;
}
int count;
map<char, TrieNode*> child;
public:
TrieNode() :count{ 0 } {};
~TrieNode() { destructTrie(this); }
void Adauga(const string& word) {
int size = (int)word.size();
auto root = this;
for (int i = 0; i < size; i++) {
auto it = (root->child).find(word[i]);
if (it == (root->child).end()) {
TrieNode* node = new TrieNode();
(root->child)[word[i]] = node;
root = node;
}
else root = (root->child)[word[i]];
}
root->count++;
}
int Count(const string& word) {
int size = (int)word.size();
auto root = this;
for (int i = 0; i < size; i++) {
auto it = (root->child).find(word[i]);
if (it == (root->child).end()) return 0;
root = (root->child)[word[i]];
}
return root->count;
}
int findPrefix(const string& word) {
int size = (int)word.size();
auto root = this;
int prefix = 0;
for (int i = 0; i < size; i++) {
auto it = (root->child).find(word[i]);
if (it == (root->child).end()) break;
root = (root->child)[word[i]];
prefix++;
}
return prefix;
}
void Sterge(const string& word) { del(this, word, 0); }
};
int main() {
TrieNode tr;
int code;
string word;
while (fin.good()) {
code = -1;
fin >> code >> word;
switch (code)
{
case 0: tr.Adauga(word);
break;
case 1:
tr.Sterge(word);
break;
case 2:
fout << tr.Count(word)<<"\n";
break;
case 3:
fout << tr.findPrefix(word)<<"\n";
break;
default:
break;
}
}
fin.close();
fout.close();
return EXIT_SUCCESS;
}
#endif //_CRT_NONSTDC_NO_WARNINGS