Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- char s[1001];
- int st[1001], top;
- /// st[1] = -1 daca e '('
- int main()
- {
- int i, x;
- ifstream fin("paranteze.in");
- fin >> s;
- fin.close();
- top = -1;
- for(i = 0; s[i] != 0; i++)
- if(s[i] == '(')
- {
- cout << "0\n";
- st[++top] = -1; /// Push(-1)
- }
- else
- {
- /// a) Ultima pe stiva este '('
- if(top >= 0 && st[top] == -1)
- {
- top--;
- if(top >= 0 && st[top] > 0)
- st[top] += 2;
- else st[++top] = 2;
- cout << st[top] << "\n";
- }
- /// b) Stiva este vida
- else if(top == -1)
- cout << "0\n";
- else
- {
- /// c.1) st = 4 ) | s = ()())
- if(top == 0)
- {
- cout << "0\n";
- top = -1;
- }
- /// c.2) st = -1 4 )
- else
- {
- x = st[top];
- top--;
- x += 2;
- top--;
- if(top >= 0 && st[top] > 0)
- st[top] += x;
- else
- st[++top] = x;
- cout << st[top] << "\n";
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement