Advertisement
smatskevich

Seminar5

Jan 11th, 2022
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <deque>
  4. #include <iostream>
  5. #include <forward_list>
  6. #include <map>
  7. #include <set>
  8. #include <stack>
  9. #include <string>
  10. #include <tuple>
  11. #include <unordered_map>
  12. #include <unordered_set>
  13. #include <vector>
  14.  
  15. typedef long long ll;
  16. using namespace std;
  17.  
  18. struct Node {
  19.   int Data;
  20.   Node* Next;
  21.  
  22.   explicit Node(int data) : Data(data), Next(nullptr) {}
  23. };
  24.  
  25. // head = 0, tail = 0
  26. //
  27. // head -> * -> * -> * -> * -> tail -> 0
  28. int main1() {
  29.   ios::sync_with_stdio(false);
  30.   cin.tie(nullptr);
  31.  
  32.   Node* head = nullptr;
  33.   Node* tail = nullptr;
  34.   int value = 0;
  35.   while (cin >> value, value != 0) {
  36.     Node* new_node = new Node(value);
  37.     if (!tail) head = tail = new_node;
  38.     else tail = tail->Next = new_node;
  39.   }
  40.   // Выводим.
  41.   for (Node* cur = head; cur; cur = cur->Next)
  42.     cout << cur->Data << " ";
  43.  
  44.   // Удаление.
  45.   for (Node* cur = head; cur;) {
  46.     Node* next = cur->Next;
  47.     delete cur;
  48.     cur = next;
  49.   }
  50.   return 0;
  51. }
  52.  
  53. // То же на базе forward_list
  54. int main2() {
  55.   ios::sync_with_stdio(false);
  56.   cin.tie(nullptr);
  57.  
  58.   forward_list<int> flist;
  59.   int value = 0;
  60.   auto it = flist.before_begin();
  61.   while (cin >> value, value != 0) {
  62.     it = flist.insert_after(it, value);
  63.   }
  64.   // Выводим.
  65.   for (auto v : flist) cout << v << " ";
  66.  
  67.   return 0;
  68. }
  69.  
  70. // Вразнобой в дек
  71. int main3() {
  72.   ios::sync_with_stdio(false);
  73.   cin.tie(nullptr);
  74.  
  75.   deque<int> d;
  76.   bool move_to_end = true;
  77.   int value = 0;
  78.   while (cin >> value, value != 0) {
  79.     if (move_to_end) d.push_back(value);
  80.     else d.push_front(value);
  81.     move_to_end = !move_to_end;
  82.   }
  83.   // Выводим.
  84.   for (auto v : d) cout << v << " ";
  85.  
  86.   return 0;
  87. }
  88.  
  89. // Проверка ПСП для круглых скобок
  90. int main4() {
  91.   ios::sync_with_stdio(false);
  92.   cin.tie(nullptr);
  93.  
  94.   int opened = 0;
  95.   string s;
  96.   cin >> s;
  97.   for (char c : s) {
  98.     if (c == '(') ++opened;
  99.     else if (c == ')') --opened;
  100.     if (opened < 0) break;
  101.   }
  102.   cout << (opened == 0 ? "YES" : "NO") << endl;
  103.  
  104.   return 0;
  105. }
  106.  
  107. // Проверка ПСП для разного типа скобок
  108. // Проверка парности двух скобок
  109. bool IsPair(char open, char close) {
  110.   if (open == '(') return close == ')';
  111.   if (open == '[') return close == ']';
  112.   if (open == '{') return close == '}';
  113.   return false;
  114. }
  115.  
  116. int main() {
  117.   ios::sync_with_stdio(false);
  118.   cin.tie(nullptr);
  119.  
  120.   stack<char> opened_braces;
  121.   string s;
  122.   cin >> s;
  123.   for (char c : s) {
  124.     if (c == '(' || c == '[' || c == '{') opened_braces.push(c);
  125.     else if (c == ')' || c == ']' || c == '}') {
  126.       if (opened_braces.empty() || !IsPair(opened_braces.top(), c)) {
  127.         cout << "NO" << endl;
  128.         return 0;
  129.       }
  130.       opened_braces.pop();
  131.     }
  132.   }
  133.   cout << (opened_braces.empty() ? "YES" : "NO") << endl;
  134.  
  135.   return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement