fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string str;
  9.  
  10. while (1)
  11. {
  12. stack<char> st;
  13. getline(cin, str);
  14. if (str[0] == '.')
  15. return 0;
  16.  
  17. if (str[0] == '.')
  18. {
  19. break;
  20. }
  21. for (int i = 0; i < str.length(); i++)
  22. {
  23. if (str[i] == '(' || str[i] == '[')
  24. st.push(str[i]);
  25.  
  26. else if (str[i] == ')')
  27. {
  28. if (st.top() != '(' || st.empty())
  29. {
  30. cout << "NO" << endl;
  31. break;
  32. }
  33. else
  34. st.pop();
  35. }
  36. else if (str[i] == ']')
  37. {
  38. if (st.top() != '[' || st.empty())
  39. {
  40. cout << "NO" << endl;
  41. break;
  42. }
  43. else
  44. st.pop();
  45. }
  46. }
  47. if (st.empty())
  48. cout << "YES" << endl;
  49. else
  50. cout << "NO" << endl;
  51. }
  52. return 0;
  53. }
Runtime error #stdin #stdout 0s 4340KB
stdin
So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
 .
.
stdout
YES
YES
NO
NO