Advertisement
Josif_tepe

Untitled

Mar 9th, 2025
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int max_size = 1000;
  4. struct stek {
  5.     string niza[max_size];
  6.     int idx;
  7.    
  8.     void init() {
  9.         idx = -1;
  10.     }
  11.     bool isEmpty() {
  12.         if(idx == -1) {
  13.             return true;
  14.         }
  15.         else {
  16.             return false;
  17.         }
  18.     }
  19.    
  20.     bool isFull() {
  21.         if(idx == max_size - 1) {
  22.             return true;
  23.         }
  24.         else {
  25.             return false;
  26.         }
  27.     }
  28.    
  29.     void push(string x) {
  30.         if(isFull() == true) {
  31.             cout << "nema poveke prostor vo stekot" << endl;
  32.             exit(-1);
  33.         }
  34.         idx++;
  35.         niza[idx] = x;
  36.     }
  37.    
  38.     string peek() {
  39.         if(isEmpty() == true) {
  40.             cout << "nema element vo stekot" << endl;
  41.             exit(-1);
  42.         }
  43.         return niza[idx];
  44.     }
  45.    
  46.     string pop() {
  47.         if(isEmpty() == true) {
  48.             cout << "nema element vo stekot" << endl;
  49.             exit(-1);
  50.         }
  51.         string result = niza[idx];
  52.         idx--;
  53.         return result;
  54.     }
  55.     int size() {
  56.         return idx + 1;
  57.     }
  58.    
  59. };
  60.  
  61.  
  62. int main()
  63. {
  64.     int n;
  65.     cin >> n;
  66.    
  67.     stek s;
  68.     s.init();
  69.     for(int i = 0; i < n; i++) {
  70.         string patika;
  71.         cin >> patika;
  72.        
  73.         if(s.isEmpty() == true) {
  74.             s.push(patika);
  75.         }
  76.         else {
  77.             if(s.peek() != patika) {
  78.                 s.pop();
  79.             }
  80.             else {
  81.                 s.push(patika);
  82.             }
  83.         }
  84.     }
  85.     cout << s.size() << endl;
  86.    
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement