ANARC09A - Seinfeld
#include <iostream>
#include <stack>
using namespace std;
stack<char> st;
int main()
{
string s;
int c, k;
k = 1;
while(true)
{
cin >> s;
c = 0;
if(s[0] == '-')
break;
for(int i = 0; i < s.size(); i++)
{
st.push(s[i]);
//stack contains } and { then we can safely remove them from stack
if(st.top() == '}' && st.size() != 1)
{
char a = st.top();
st.pop();
char b = st.top(); // if b != { do not remove and push back a also
if(a != b)
st.pop();
else
st.push(a);
}
}
while(!st.empty())
{
char a = st.top();
st.pop();
char b = st.top();
st.pop();
if(a == b)
c++;
else // for }{ 2 operations are required
c += 2;
}
cout << k << ". " << c << endl;
k++;
}
return 0;
}
Comments
Post a Comment