From 9477dbe667f250ecd23f8fc0d56b942191526421 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Thu, 25 Feb 2021 14:42:55 +0100 Subject: Stare semestry, niepoukladane --- Semestr 3/pf/lista7/zaj.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Semestr 3/pf/lista7/zaj.cpp (limited to 'Semestr 3/pf/lista7/zaj.cpp') diff --git a/Semestr 3/pf/lista7/zaj.cpp b/Semestr 3/pf/lista7/zaj.cpp new file mode 100644 index 0000000..403754f --- /dev/null +++ b/Semestr 3/pf/lista7/zaj.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +const int ALPHABET_SIZE = 26; + +struct node +{ + node *children[ALPHABET_SIZE]; + int cnt; + bool is_end; + + node() + { + for (int i = 0; i < ALPHABET_SIZE; i++) + children[i] = nullptr; + is_end = false; + cnt = 0; + } +}; + +struct TRIE +{ + node *root; + + TRIE() + { + root = new node; + } + + // dodaje słowo do słownika + void add_word(string word) + { + node *crawl = root; + for (auto c : word) + { + int letter = c - 'a'; + if (crawl->children[letter] == nullptr) + { + crawl->children[letter] = new node; + } + crawl = crawl->children[letter]; + } + crawl->is_end = true; + } + + // sprawdź czy słowo jest w słowniku + bool find(string word) + { + node *crawl = root; + for (auto c : word) + { + int letter = c - 'a'; + if (crawl->children[letter] == nullptr) + return false; + crawl = crawl->children[letter]; + } + return crawl->is_end; + } + + // usuń słowo ze słownika + void erase(string word) {} +}; + +int main() +{ + TRIE dictionary; + int q; + cin >> q; + while (q--) + { + char c; + string s; + cin >> c >> s; + if (c == 'A') + dictionary.add_word(s); + else + cout << (dictionary.find(s) ? "TAK" : "NIE") + << "\n"; + } +} \ No newline at end of file -- cgit v1.2.3