fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define GG ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  4. int main () { GG;
  5. string s;
  6. cin>>s; int n=s.size(),ans=0;
  7. stack <char> st; bool f=0;
  8.  
  9. for(int i=0;i<n;i++)
  10. { if( (s[i]=='(' ) || (s[i]=='{' ) || (s[i]=='[' ) || (s[i]=='<' ) )
  11. st.push(s[i]);
  12. else if(st.size() != 0)
  13. { if ( st.top()=='(' && s[i]==')')
  14. st.pop();
  15. else if(st.top()=='{' && s[i]=='}')
  16. st.pop();
  17. else if(st.top()=='[' && s[i]==']')
  18. st.pop();
  19. else if(st.top()=='<' && s[i]=='>')
  20. st.pop();
  21.  
  22. else
  23. { st.pop();
  24. ans++;
  25. }
  26. }
  27. else
  28. { f=1; break;
  29. }
  30. }
  31.  
  32. if(f || st.size()!=0 ) cout<<"Impossible\n";
  33. else cout<<ans<<"\n";
  34. return 0;}
Success #stdin #stdout 0.01s 5308KB
stdin
<<
stdout
Impossible