From a31e5d9f655e4cbc0ea620d69c5fdafd3406f623 Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:09:01 -0300 Subject: [PATCH] brackets, stack, queue, deque, maps and sets --- theory/data_structure/bracket_matching.cpp | 77 +++++++++++++++++++ .../data_structure/custom_comparators_STL.cpp | 17 ++++ theory/data_structure/dictionary.cpp | 29 +++++++ theory/data_structure/queues.cpp | 35 +++++++++ theory/data_structure/unordered_map_set.cpp | 36 +++++++++ 5 files changed, 194 insertions(+) create mode 100644 theory/data_structure/bracket_matching.cpp create mode 100644 theory/data_structure/custom_comparators_STL.cpp create mode 100644 theory/data_structure/dictionary.cpp create mode 100644 theory/data_structure/queues.cpp create mode 100644 theory/data_structure/unordered_map_set.cpp diff --git a/theory/data_structure/bracket_matching.cpp b/theory/data_structure/bracket_matching.cpp new file mode 100644 index 0000000..826defd --- /dev/null +++ b/theory/data_structure/bracket_matching.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +std::vector stringToDigit(char *s1) { + + std::vector ans; + + for(int i = 0; s1[i] != '\0'; ++i) { + switch(s1[i]) { + case '{': + ans.emplace_back(1); + break; + case '}': + ans.emplace_back(2); + break; + case '(': + ans.emplace_back(3); + break; + case ')': + ans.emplace_back(4); + break; + case '[': + ans.emplace_back(5); + break; + case ']': + ans.emplace_back(6); + break; + default: + break; + } + } + + return ans; + +} + +bool valid(std::vector &brackets) { + + std::stack st; + int N = (int)brackets.size(); + + for(int i = 0; i < N; ++i) { + if(st.empty()) { + if(brackets[i] % 2 != 0) + st.push(brackets[i]); + else + return false; + } + else if (st.top() == brackets[i] - 1 && brackets[i] % 2 == 0) + st.pop(); + else if (brackets[i] % 2 != 0) + st.push(brackets[i]); + else + return false; + } + + return st.empty(); + +} + +int main(void) { + + std::vector brackets; + int n; + std::cin >> n; + + for(int i = 0; i < n; ++i) { + char s[300]; + std::cin >> s; + brackets = stringToDigit(s); + if(valid(brackets)) + std::cout << "Yes" << std::endl; + else + std::cout << "No" << std::endl; + } +} diff --git a/theory/data_structure/custom_comparators_STL.cpp b/theory/data_structure/custom_comparators_STL.cpp new file mode 100644 index 0000000..6e9692a --- /dev/null +++ b/theory/data_structure/custom_comparators_STL.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + + + +int main(){ + + std::vector nums = {3,4,2,1,3,4,5,7,8,9,93,90,87,68}; + + +} + + + diff --git a/theory/data_structure/dictionary.cpp b/theory/data_structure/dictionary.cpp new file mode 100644 index 0000000..58aeba9 --- /dev/null +++ b/theory/data_structure/dictionary.cpp @@ -0,0 +1,29 @@ +#include +#include + +/* + A dictionary is a translator from one type to another + (aplication of maps) +*/ + +std::unordered_map dict; + + + +int main(){ + + // converting a session of chars to int; + dict['{'] = 1; + dict['}'] = 2; + dict['('] = 3; + dict[')'] = 4; + dict['['] = 5; + dict[']'] = 6; + + // we Can do the inverse operation, transforming each number + // in a corresponding char + + for(std::unordered_map::iterator it = dict.begin(); it != dict.end(); ++it) + std::cout << "key: " << it->first << "\n" << "element: " << it->second << std::endl; + +} \ No newline at end of file diff --git a/theory/data_structure/queues.cpp b/theory/data_structure/queues.cpp new file mode 100644 index 0000000..480d9d3 --- /dev/null +++ b/theory/data_structure/queues.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +/* + # Queues, Deques and Priority Queues + +-> Queues, data structure based in fifo "first in, first out", + we've a STL implementation in C++. +*/ + + +int main(){ + + // Deques (like a double-linked-list) + std::deque deq; + deq.push_back(1); // {1} + deq.push_front(2); // {2,1} + deq.push_back(3); // {2,1,3} + + std::cout << deq.front() << "\n"; // {2} + deq.back(); // get's the element in last position + deq.front(); // get's the element in firts position + + // Priority queues + std::priority_queue pq; + pq.push(1); // O(log n) + pq.push(2); + pq.push(3); + // pq = {1,2,3} + pq.top(); // {3} + + +} \ No newline at end of file diff --git a/theory/data_structure/unordered_map_set.cpp b/theory/data_structure/unordered_map_set.cpp new file mode 100644 index 0000000..ac2ebe1 --- /dev/null +++ b/theory/data_structure/unordered_map_set.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + + + +int main(){ + + // Unordered map operations + std::unordered_map zipcode; + zipcode.emplace("Blanca",33456); // Insertion O(1) + zipcode["Blanca"] = 33009; // change O(1) + + if(zipcode.find("Blanca") != zipcode.end()) // search O(1) + std::cout << "Found\n"; + else + std::cout << "Not Found\n"; + //zipcode.erase("Blanca"); // deletion O(1) + + for(const auto &element: zipcode) + std::cout << "key: " << element.first << " " << "value: " << element.second << std::endl; + + // Unordered set operations + // same functions as unordered map + std::unordered_set friends; + friends.insert("Julia"); + friends.insert("Martha"); + friends.erase("Julia"); + if(friends.find("Julia") != friends.end()) + std::cout << "Found\n"; + else + std::cout << "Not found\n"; + + +} +