chillurbrain

10. Скобки

May 22nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. int main()
  5. {
  6. stack<char> s;
  7. char c;
  8. int n;
  9. bool t =true;
  10. cin >> n;
  11. for (int i =0 ; i < n ; i++) {
  12. cin >> c;
  13. if (c == '(' || c == '[' || c == '{') {
  14. s.push(c);
  15. }
  16. else {
  17. if (s.empty()) {
  18. t = false;
  19. break;
  20. }
  21. if (c == ')' && s.top() == '(' || c == '}' && s.top() == '{' || c == ']' && s.top() == '[') {
  22. s.pop();
  23. }
  24. else {
  25. t = false;
  26. break;
  27. }
  28. }
  29. }
  30. if (s.empty() && t) {
  31. cout << "Yes";
  32. }
  33. else {
  34. cout << "No";
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment