B.cpp 962 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int flag;
  6. stack<char> s;
  7. string str;
  8. while(cin>>str){
  9. flag=1;
  10. for(int i=0;i<str.size();i++){
  11. if(str[i]=='('||str[i]=='['||str[i]=='{'){
  12. s.push(str[i]);
  13. }
  14. else{
  15. if(!s.empty()){
  16. if((str[i]==')'&&s.top()=='(')||(str[i]==']'&&s.top()=='[')||(str[i]=='}'&&s.top()=='{')){
  17. s.pop();
  18. }
  19. else if((str[i]==')'&&!s.empty())||(str[i]==']'&&!s.empty())||(str[i]=='}'&&!s.empty())){
  20. flag=0;
  21. }
  22. }else{
  23. flag=0;
  24. }
  25. }
  26. }
  27. if(!s.empty()||flag==0){
  28. cout<<"NO"<<endl;
  29. }else{
  30. cout<<"YES"<<endl;
  31. }
  32. while(!s.empty()){
  33. s.pop();
  34. }
  35. str.clear();
  36. }
  37. }