Advertisement
J00ker

Untitled

Mar 10th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. char s[1001];
  7. int st[1001], top;
  8. /// st[1] = -1 daca e '('
  9.  
  10. int main()
  11. {
  12. int i, x;
  13. ifstream fin("paranteze.in");
  14. fin >> s;
  15. fin.close();
  16.  
  17. top = -1;
  18. for(i = 0; s[i] != 0; i++)
  19. if(s[i] == '(')
  20. {
  21. cout << "0\n";
  22. st[++top] = -1; /// Push(-1)
  23. }
  24. else
  25. {
  26. /// a) Ultima pe stiva este '('
  27. if(top >= 0 && st[top] == -1)
  28. {
  29. top--;
  30. if(top >= 0 && st[top] > 0)
  31. st[top] += 2;
  32. else st[++top] = 2;
  33. cout << st[top] << "\n";
  34. }
  35. /// b) Stiva este vida
  36. else if(top == -1)
  37. cout << "0\n";
  38. else
  39. {
  40. /// c.1) st = 4 ) | s = ()())
  41. if(top == 0)
  42. {
  43. cout << "0\n";
  44. top = -1;
  45. }
  46. /// c.2) st = -1 4 )
  47. else
  48. {
  49. x = st[top];
  50. top--;
  51. x += 2;
  52. top--;
  53. if(top >= 0 && st[top] > 0)
  54. st[top] += x;
  55. else
  56. st[++top] = x;
  57. cout << st[top] << "\n";
  58. }
  59. }
  60.  
  61. }
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement