Advertisement
a53

Valoare

a53
Apr 20th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("valoare.in");
  4. ofstream fout("valoare.out");
  5. int C, N, fr[27];
  6. char S[1005];
  7. int top, nr, ok;
  8. long long st[1005], suma;
  9. int main()
  10. {
  11. fin>>C;
  12. if(C==1)
  13. {
  14. char ch;
  15. while(fin >> ch)
  16. {
  17. if(ch>='A' && ch<='Z')
  18. fr[ch-'A'+1]++;
  19. }
  20. for(int i=1; i<=26; ++i)
  21. if(fr[i])
  22. N++;
  23. fout<<N;
  24. return 0;
  25. }
  26. if(C==2)
  27. {
  28. char ch;
  29. int sum=0, nr=0;
  30. while(fin>>ch)
  31. {
  32. if(isdigit(ch))
  33. nr=nr*10+(ch-48);
  34. else
  35. {
  36. sum=sum+nr;
  37. nr=0;
  38. }
  39. }
  40. if(nr)
  41. sum+=nr;
  42. fout<<sum;
  43. return 0;
  44. }
  45. fin.get();
  46. fin.getline(S, 1005);
  47. if(S[3]=='L')
  48. {
  49. for(int i=0; i<(int)strlen(S); i++)
  50. {
  51. if(S[i]=='(')
  52. {
  53. top++;
  54. st[top]=-1;
  55. }
  56. else if(S[i]>='A' && S[i]<='Z')
  57. {
  58. while(S[i]>='A' && S[i]<='Z')
  59. {
  60. top++;
  61. st[top]=(int)(S[i]-'A'+1);
  62. i++;
  63. }
  64. i--;
  65. }
  66. else if(S[i]==')')
  67. {
  68. if(S[i-1]>='A' && S[i-1]<='Z')
  69. {
  70. suma=0;
  71. while(st[top]!=-1)
  72. {
  73. suma+=st[top];
  74. st[top]=0;
  75. top--;
  76. }
  77. st[top]=suma;
  78. }
  79. }
  80. else if(S[i]>='0' && S[i]<='9')
  81. {
  82. if(S[i-2]>='A' && S[i-2]<='Z')
  83. ok=1;
  84. int nr=0;
  85. while(S[i] && (S[i]>='0' && S[i]<='9'))
  86. nr=nr*10+(S[i]-48), i++;
  87. i--;
  88. if(ok)
  89. st[top]=st[top]*nr;
  90. else
  91. {
  92. int pr=top;
  93. while(st[pr]!=-1)
  94. {
  95. st[pr]=st[pr]*nr;
  96. pr--;
  97.  
  98. }
  99. }
  100. }
  101. ok=0;
  102. }
  103. suma=0;
  104. for(int i=1; st[i]; i++)
  105. {
  106. if(st[i]>0)
  107. suma+=st[i];
  108. }
  109. fout<<suma;
  110. return 0;
  111. }
  112. for(int i=0; i<(int)strlen(S); i++)
  113. {
  114. if(S[i]=='(')
  115. {
  116. top++;
  117. st[top]=-1;
  118. }
  119. else if(S[i]>='A' && S[i]<='Z')
  120. {
  121. while(S[i]>='A' && S[i]<='Z')
  122. {
  123. top++;
  124. st[top]=(int)(S[i]-'A'+1);
  125. i++;
  126. }
  127. i--;
  128. }
  129. else if(S[i]==')')
  130. {
  131. suma=0;
  132. while(st[top]!=-1)
  133. {
  134. suma+=st[top];
  135. st[top]=0;
  136. top--;
  137. }
  138. st[top]=suma;
  139. }
  140. else if(S[i]>='0' && S[i]<='9')
  141. {
  142. int nr=0;
  143. while(S[i] && (S[i]>='0' && S[i]<='9'))
  144. nr=nr*10+(S[i]-48), i++;
  145. i--;
  146. st[top]=st[top]*nr;
  147. }
  148. }
  149. if(top==1)
  150. fout<<st[top];
  151. else
  152. {
  153. suma=0;
  154. for(int i=1; st[i]; i++)
  155. if(st[i]>0)
  156. suma+=st[i];
  157. fout<<suma;
  158. }
  159. return 0;
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement