Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <climits>
- #define Nmax 1000
- using namespace std;
- struct Stiva
- {
- int st[Nmax];
- int top;
- void Init()
- {
- top = -1;
- }
- int Empty()
- {
- if(top == -1) return 1;
- return 0;
- }
- int Full()
- {
- if(top == Nmax - 1) return 1;
- return 0;
- }
- void Push(int x)
- {
- if(Full()) cout << "Stack Overflow!";
- else st[++top] = x;
- }
- void Pop()
- {
- if(!Empty()) top--;
- }
- int Top()
- {
- return st[top];
- }
- int Size()
- {
- return top+1;
- }
- };
- int main()
- {
- int n;
- char s[100];
- Stiva st;
- ifstream fin("stiva.in");
- fin >> n;
- st.Init();
- for(int i = 1; i <= n; i++)
- {
- int x;
- fin >> s;
- if(s == '(') st.Push(-1);
- else
- {
- if(st.Size() == 0) /// Stiva este vida
- cout << "0\n";
- else if(st.Top() == -1) /// a)
- {
- st.Pop();
- if(st.Size() > 0 && st.Top() > 0)
- {
- x = st.Top();
- x += 2;
- st.Pop();
- st.Push(x);
- }
- else st.Push(2);
- cout << st.Top() << "\n";
- }
- else /// b)In varful sitvei este o valoare > 0
- {
- x = st.Top();
- st.Pop();
- if(st.Size() > 0)
- {
- x += 2;
- st.Pop;
- st.Push(x);
- }
- if(st.Size() > 0)
- cout << st.Top() << "\n";
- else cout << "0\n";
- }
- }
- }
- fin.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement