• Source
    1. /*
    2. Author : Shohanur Rahaman
    3. */
    4.  
    5. #include<stack>
    6. #include<cstdio>
    7. #include<iostream>
    8. #include<string>
    9. using namespace std;
    10.  
    11. bool checkMatch(char open,char close)
    12. {
    13. if(open == '(' && close == ')') return true;
    14. else if(open == '{' && close == '}') return true;
    15. else if(open == '[' && close == ']' ) return true;
    16. else false;
    17. }
    18.  
    19. bool checkBalance(string str)
    20. {
    21. stack <char> mystack;
    22.  
    23. for(int i=0;i<str.length();i++){
    24. if(str[i] == '(' || str[i] == '{' || str[i] == '['){
    25. mystack.push(str[i]);
    26. }
    27. else if(str[i]==')' || str[i]=='}' || str[i] == ']'){
    28. if(mystack.empty() || !checkMatch(mystack.top(),str[i]))
    29. return false;
    30. else
    31. mystack.pop();
    32. }
    33. }
    34. if(mystack.empty() == true)
    35. return true;
    36. else
    37. return false;
    38.  
    39. }
    40.  
    41. int main()
    42. {
    43. string exp;
    44.  
    45. while(cin>>exp){
    46.  
    47. if(checkBalance(exp)){
    48. cout<<"correct"<<endl;
    49. }
    50. else
    51. cout<<"incorrect"<<endl;
    52. }
    53.  
    54.  
    55. return 0;
    56. }
    57.  
    58.  
    59.  
    60.