fork download
  1. #include<bits/stdc++.h>
  2. #define s(x) scanf("%d",&(x))
  3. #define print(x) printf("%d ",x)
  4. #define printnl(x) printf("%d\n",x);
  5. #define forI(i,a,b) for(int i=(a);i<=(b);i++)
  6. #define forD(i,a,b) for(int i=(a);i>=(b);--i)
  7. #define MOD 1000000009
  8. #define INF_LONG 8999999999999999999
  9. #define ll long long
  10. #define FOUND 1
  11. #define NOTFOUND 0
  12. #define MAXSIZE 10000
  13. #define pb push_back
  14. using namespace::std;
  15.  
  16. typedef vector <int> vi;
  17. typedef pair < int, int > ii;
  18. typedef vector < ii > vii;
  19. typedef vector <vii> vvii;
  20.  
  21. ll unsigned valid_count(string &s)
  22. {
  23. deque <char> d;
  24. ll unsigned c = 0;
  25. for(auto it : s)
  26. {
  27. if(it == '<')
  28. d.pb(it);
  29. else if(it == '>')
  30. {
  31. if(!d.empty() && d.back() == '<')
  32. {
  33. d.pop_back();
  34. c += 2;
  35. }
  36. else return c; //first mismatching '>' encountered
  37. }
  38. else return c; //char other than '<' and '>'
  39. }
  40. return c;
  41. }
  42. int main()
  43. {
  44. int T;
  45. vector <char> v;
  46. string str;
  47. //cout<<"\nenter the no of test cases :\t";
  48. cin>>T;
  49. while(T--)
  50. {
  51. // cout<<"\nenter the string :\t";
  52. cin>>str;
  53.  
  54. // cout<<"\nvalid count is :\t";
  55. cout<<valid_count(str)<<endl;
  56. }
  57. }
  58.  
Success #stdin #stdout 0s 3436KB
stdin
1
<<><>>
stdout
6