fork download
  1. #include <iostream>
  2. #include <string>
  3. #include<stack>
  4.  
  5. using namespace std;
  6.  
  7. int operand(char ch)
  8. {
  9. return (ch>='a' && ch<='z') || (ch>='A' && ch<='Z');
  10. }
  11.  
  12. int prec(char ch)
  13. {
  14. switch(ch)
  15. {
  16. case '+':
  17. case '-':
  18. return 1;
  19. case '*':
  20. case '/':
  21. return 2;
  22. case '^':
  23. return 3;
  24. }
  25. return -1;
  26. }
  27. int postfix(string infix,stack <char> s)
  28. {
  29. for(int i=0; i<infix.length(); i++)
  30. {
  31. if(operand(infix[i]))
  32. cout<<infix[i];
  33. else if(infix[i]=='(')
  34. s.push(infix[i]);
  35. else if(infix[i]==')')
  36. {
  37. while((!s.empty()) && (s.top()!='('))
  38. {
  39. if(s.top()!='(' && s.top()!=')')
  40. cout<<s.top();
  41. s.pop();
  42. }
  43. if(s.empty() && s.top()!='(')
  44. return 0;
  45. }
  46. else
  47. {
  48. while((!s.empty()) && prec(infix[i])<=prec(s.top()))
  49. {
  50. if(s.top()!='(' && s.top()!=')')
  51. cout<<s.top();
  52. s.pop();
  53.  
  54. }
  55. s.push(infix[i]);
  56.  
  57. }
  58.  
  59.  
  60. }
  61. while(!(s.empty()))
  62. {
  63. if(s.top()!='(' && s.top()!=')')
  64. cout<<s.top();
  65. s.pop();
  66. }
  67. }
  68.  
  69. int balance(string infix)
  70. {
  71. stack <char> s;
  72. for(int i=0; i<infix.length(); i++)
  73. {
  74.  
  75. if(infix[i] == '(' || infix[i] == '[' || infix[i] == '{' )
  76. {
  77. s.push(infix[i]);
  78. }
  79. else if(infix[i] == ')' || infix[i] == '}' || infix[i] == ']' )
  80. {
  81. if(s.empty())
  82. return 0;
  83. else if(infix[i] == ')')
  84. {
  85. if(s.top() == '(')
  86. {
  87. s.pop();
  88. }
  89. else
  90. return 0;
  91.  
  92. }
  93. else if(infix[i] == ']')
  94. {
  95. if(s.top() == '[')
  96. {
  97. s.pop();
  98. }
  99. else
  100. return 0;
  101.  
  102. }
  103. else if(infix[i] == '}')
  104. {
  105. if(s.top() == '{')
  106. {
  107. s.pop();
  108. }
  109. else
  110. return 0;
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117. }
  118. if(s.empty())
  119. return 1;
  120. else
  121. return 0;
  122.  
  123. }
  124. int main() {
  125. string infix;
  126. cin>>infix;
  127. //stack <char> s;
  128. //postfix(infix,s);
  129. int ans = balance(infix);
  130. cout<<ans;
  131. return 0;
  132. }
Success #stdin #stdout 0s 16072KB
stdin
#include <iostream>
#include <string>
#include<stack>	

using namespace std;

int operand(char ch)
{
   return (ch>='a' && ch<='z') || (ch>='A' && ch<='Z');
}

int prec(char ch)
{
  switch(ch)
  {
    case '+':
    case '-':
	return 1;
    case '*':
    case '/':
	return 2;
    case '^':
	return 3;
  }
  return -1;
}
int postfix(string infix,stack <char> s)
{
	for(int i=0; i<infix.length(); i++)
	{
	   if(operand(infix[i]))
	    cout<<infix[i];
	   else if(infix[i]=='(')
	    s.push(infix[i]);
	   else if(infix[i]==')')
	   {
	   	while((!s.empty()) && (s.top()!='('))
	   	{
	   	  if(s.top()!='(' && s.top()!=')')	
	   	  cout<<s.top();
	   	  s.pop();
	   	}
	   	if(s.empty() && s.top()!='(')
	   	return 0;
	   }
	   else
	   {
	      while((!s.empty()) && prec(infix[i])<=prec(s.top()))
	      {
	      	if(s.top()!='(' && s.top()!=')')
	      	cout<<s.top();
	      	s.pop();
	      	
	      }
	      s.push(infix[i]);
	     
	   }
	   
	   
	}
	while(!(s.empty()))
	{
		if(s.top()!='(' && s.top()!=')')
		cout<<s.top();
		s.pop();
	}
}

int balance(string infix)
{
	stack <char> s;
	for(int i=0; i<infix.length(); i++)
	{
		
		if(infix[i] == '(' || infix[i] == '[' || infix[i] == '{' )
		 {
		 	s.push(infix[i]);
		 }
		else
		 {
		 	if(s.empty())
		 	 return 0;
		 	else if(infix[i] == ')')
		 	{
			 	if(s.top() == '(')
			 	  {
			 	  	s.pop();
			 	  }
			 	  else 
		 			return 0;
			 	  
		 	}
		 	else if(infix[i] == ']')
		 	{
			 	if(s.top() == '[')
			 	  {
			 	  	s.pop();
			 	  }
			 	  else 
		 			return 0;
			 	  
		 	}
		 	else if(infix[i] == '}')
		 	{
			 	if(s.top() == '{')
			 	  {
			 	  	s.pop();
			 	  }
			 	  else 
		 			return 0;
			 	  
		 	}
		 	
			 
		 }
	}
	if(s.empty())
	 return 1;
	else
	 return 0;
	 
}
int main() {
	string infix;
	cin>>infix;
	//stack <char> s;
	//postfix(infix,s);
	int ans = balance(infix);
	cout<<ans;
	return 0;
}
stdout
1