Advertisement
maxim_shlyahtin

lb4

May 28th, 2023
1,080
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 1 0
  1. class CustomStack {
  2. public:
  3. // конструкторы и деструктор
  4.     CustomStack() {
  5.         mCapacity = 10; // начальная ёмкость стека
  6.         mSize = 0;
  7.         mData = new char *[mCapacity];
  8.     }
  9.  
  10.     explicit CustomStack(int capacity) {
  11.         mCapacity = capacity;
  12.         mSize = 0;
  13.         mData = new char *[mCapacity];
  14.     }
  15.  
  16.     ~CustomStack() {
  17.         for (size_t i = 0; i < mSize; ++i) {
  18.             delete[] mData[i];
  19.         }
  20.         delete[] mData;
  21.     }
  22.  
  23. // методы действий со стеком
  24.     void push(const char *val) {
  25.         if (mSize >= mCapacity) {
  26.             extend(mCapacity); // увеличиваем ёмкость стека при необходимости
  27.         }
  28.         mData[mSize] = new char[strlen(val) + 1];
  29.         strcpy(mData[mSize], val);
  30.         ++mSize;
  31.     }
  32.  
  33.     void pop() {
  34.         if (!empty()) {
  35.             delete[] mData[mSize - 1];
  36.             --mSize;
  37.         }
  38.     }
  39.  
  40.     char *top() {
  41.         if (!empty()) {
  42.             return mData[mSize - 1];
  43.         }
  44.         return nullptr;
  45.     }
  46.  
  47.     size_t size() {
  48.         return mSize;
  49.     }
  50.  
  51.     bool empty() {
  52.         return mSize == 0;
  53.     }
  54.  
  55.     void extend(int n) {
  56.         mCapacity += n;
  57.         char **newData = new char *[mCapacity];
  58.         for (size_t i = 0; i < mSize; ++i) {
  59.             newData[i] = mData[i];
  60.         }
  61.         delete[] mData;
  62.         mData = newData;
  63.     }
  64.  
  65. private:
  66.     size_t mCapacity{}; // максимальная ёмкость стека
  67.     size_t mSize{}; // количество элементов в стеке
  68. protected:
  69.     char **mData; // указатель на массив данных
  70. };
  71.  
  72. int main() {
  73.     string data;
  74.     getline(cin, data);
  75.     int count_open_tags = 0;
  76.     int count_closing_tags = 0;
  77.     for (int i = 1; i < data.size(); i++) {
  78.         if (data[i - 1] == '<' && data[i] != '/')
  79.             count_open_tags++;
  80.         else if(data[i - 1] == '<' && data[i] == '/')
  81.             count_closing_tags++;
  82.     }
  83.     CustomStack open_tags(count_open_tags);
  84.     for (int i = 0; i < data.size(); i++) {
  85.         char res[10];
  86.         res[0] = '\0';
  87.         if (data[i] == '<'){
  88.             int j = i + 1, n = 0;
  89.             while(data[j] != '>') {
  90.                 res[n] = data[j];
  91.                 n++; j++;
  92.             }
  93.             res[n] = '\0';
  94.             if(res[0] == '/'){
  95.                 char *check = open_tags.top();
  96.                 for(int k = 1; res[k]; k++)
  97.                     if(check[k - 1] != res[k]){
  98.                         cout << "wrong";
  99.                         return 0;
  100.                     }
  101.                 open_tags.pop();
  102.             }
  103.             else if(strcmp(res, "br") != 0 && strcmp(res, "hr") != 0)
  104.                 open_tags.push(res);
  105.             i = j;
  106.         }
  107.     }
  108.     cout << "correct";
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement