Question - SPOJ.com - Problem ANARC09A
#include<iostream>
#include<stack>
using namespace std;
int main()
{
while(1){
string a;
cin>>a;
// cout<<a<<endl;
if(a[0]=='-')
break;
stack<int> s;
for(int i = 0;a[i]!='\0';i++)
{
if(a[i]=='{')
s.push(1);
else
{
if(s.size()==0)
s.push(-1);
else
{
if(s.top()==1)
s.pop();
else
s.push(-1);
}
}
}
int right = 0,left = 0;
while(!s.empty())
{
if(s.top()==1)
right++;
else
left++;
s.pop();
}
if(left == 0)
cout<<right/2<<endl;
else if(right == 0)
cout<<left/2<<endl;
else
cout<<left+right<<endl;
// cout<<endl;
}
return 0;
}
Whatβs my approach is
- Remove the β{}β like group in the string so we only have a string with unbalanced
- Count β{β in the string and β}β also
- If the string does not contain β{β then answer is count of β}β/2 and vice- versa 4. If string contain both then answer is count of bothβ¦ What is wrong in this approach