Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <assert.h>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- ///стэк целых чисел, реализованный с помощью массива
- class CStack {
- public:
- CStack (int _bufferSize); ///конструктор
- ~CStack(); ///деструктор
- ///добавленние и извлечение элемента из стека
- void Push ( char a );
- char Pop();
- ///проверка на пустоту
- bool IsEmpty() const { return top == -1;}
- private:
- char* buffer;
- int bufferSize;
- int top; ///индекс верхнего элемента
- };
- CStack::CStack( int _bufferSize) : bufferSize (_bufferSize ), top ( -1 )
- {
- buffer = new char[bufferSize]; ///создаём буффер
- }
- CStack::~CStack()
- {
- delete[] buffer; ///удаляем буффер
- }
- ///добавление элемента
- void CStack::Push( char a )
- {
- assert( top + 1 < bufferSize );
- buffer[++top] = a;
- }
- ///извлечение элемента
- char CStack::Pop()
- {
- assert( top!= -1);
- return buffer[top--];
- }
- char* solve(char *a);
- int main()
- {
- char a[800005];
- gets(a);
- cout << solve(a);
- return 0;
- }
- char* solve(char *a){
- char q;
- CStack f(strlen(a));
- int razm = 0;
- int y = strlen(a);
- char *t = new char[800005];
- for (int i = 0; i < y; i++)
- if (a[i] == '(' || a[i] == '[' || a[i] == '{')
- {
- f.Push(a[i]);
- razm++;
- }
- else
- {
- bool pr = 0;
- if (razm==0)
- {
- f.Push(a[i]);
- razm++;
- pr = 1;
- }
- else
- {
- q=f.Pop();
- razm--;
- if(q == ')' || q == ']' || q == '}')
- {
- f.Push(q);
- f.Push(a[i]);
- razm+=2;
- pr = 1;
- }
- else
- {
- if (a[i]==')' && q=='(')
- pr = 1;
- if (a[i]==']' && q=='[')
- pr = 1;
- if (a[i]=='}' && q=='{')
- pr = 1;
- }
- if (pr == 0)
- {
- return "IMPOSSIBLE";
- return 0;
- }
- }
- }
- if (razm == 0)
- {
- for (int i = 0; i < y; i++)
- return a;
- }
- char b[80005];
- int b1 = 0;
- char w = f.Pop();
- razm--;
- while (w == '(' || w == '[' || w == '{')
- {
- if (w == '(')
- {b[b1] = ')';b1++;}
- if (w == '[')
- {b[b1] = ']';b1++;}
- if (w == '{')
- {b[b1] = '}';b1++;}
- if (razm == 0) break;
- else w = f.Pop();
- razm--;
- }
- razm++;
- int t1 = 0;
- while (razm > 0){
- if (w == ')')
- {t[t1] = '(';t1++;}
- if (w == ']')
- {t[t1] = '[';t1++;}
- if (w == '}')
- {t[t1] = '{';t1++;}
- razm--;
- if (razm == 0) break;
- else w = f.Pop();
- }
- t = strcat(t,a);
- t = strcat(t,b);
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement