fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. string xau;
  9. cin>>xau;
  10. stack <int> s;
  11. int ktAdd=0;
  12. for (int i=0; i<xau.length(); i++)
  13. {
  14. if (xau[i]=='(')
  15. {
  16. s.push(0);
  17. }
  18. else if (xau[i]==')')
  19. {
  20. int tmp=0;
  21. while (!s.empty() && s.top()!=0)
  22. {
  23. tmp+=s.top();
  24. s.pop();
  25. }
  26. if (!s.empty() && s.top()==0)
  27. {
  28. s.pop();
  29. s.push(tmp);
  30. }
  31. }
  32. else if (xau[i]>='0' && xau[i]<='9')
  33. {
  34. int so=xau[i]-'0';
  35. if (!s.empty())
  36. {
  37. int tmp = s.top();
  38. s.pop();
  39. tmp = tmp * so;
  40. s.push(tmp);
  41. } else
  42. {}
  43. }
  44. else if (xau[i]=='C')
  45. {
  46. s.push(12);
  47. }
  48. else if (xau[i]=='H')
  49. {
  50. s.push(1);
  51. }
  52. else if (xau[i]=='O')
  53. {
  54. s.push(16);
  55. }
  56. }
  57. int S=0;
  58. while (!s.empty())
  59. {
  60. S+=s.top();
  61. s.pop();
  62. }
  63. cout<<S;
  64. return 0;
  65. }
Success #stdin #stdout 0s 15240KB
stdin
((CH)2(OH2H)(C(H))O)3
stdout
222