Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <climits>
  4. #include <cstring>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int n;
  10. char s[30];
  11. int total;
  12. int ans = INT_MIN;
  13. bool check[30];
  14. bool visit[30];
  15.  
  16. int stringtoint(string num) {
  17. int ans = 0;
  18. if (num[0] == '-') {
  19. for (int i = 1; i < num.size(); i++) {
  20. ans = ans * 10 + (num[i] - '0');
  21. }
  22. }
  23. else {
  24. for (int i = 0; i < num.size(); i++) {
  25. ans = ans * 10 + (num[i] - '0');
  26. }
  27. }
  28.  
  29. if (num[0] == '-') ans = -ans;
  30. return ans;
  31. }
  32.  
  33. void go(int cnt) {
  34. if (cnt == total) {
  35. vector<string> newstr;
  36. for (int i = 0; i < n; i++) {
  37. if (!visit[i]) {
  38. string t = "";
  39. t += s[i];
  40. newstr.push_back(t);
  41. }
  42. else {
  43. int a = s[i] - '0', b = s[i + 2] - '0';
  44. char op = s[i + 1];
  45. int ans;
  46. if (op == '+') {
  47. ans = a + b;
  48. newstr.push_back(to_string(ans));
  49. }
  50. else if (op == '-') {
  51. ans = a - b;
  52. newstr.push_back(to_string(ans));
  53. }
  54. else if (op == '*') {
  55. ans = a * b;
  56. newstr.push_back(to_string(ans));
  57. }
  58. i += 2;
  59. }
  60. }
  61.  
  62. int tmp = stringtoint(newstr[0]);
  63. int ssize = newstr.size();
  64. for (int i = 0; i < ssize / 2; i++) {
  65. string op = newstr[2 * i + 1];
  66. if (op == "+") {
  67. tmp = tmp + stringtoint(newstr[2 * i + 2]);
  68. }
  69. else if (op == "-") {
  70. tmp = tmp - stringtoint(newstr[2 * i + 2]);
  71. }
  72. else if (op == "*") {
  73. tmp = tmp * stringtoint(newstr[2 * i + 2]);
  74. }
  75. }
  76. if (tmp > ans) {
  77. ans = tmp;
  78. }
  79. return;
  80. }
  81.  
  82. go(cnt + 1);
  83. if (cnt == 0 || (cnt - 1 >= 0 && check[cnt - 1] == false)) {
  84. check[cnt] = true;
  85. visit[2 * cnt] = true;
  86. visit[2 * cnt + 1] = true;
  87. visit[2 * cnt + 2] = true;
  88. go(cnt + 1);
  89. check[cnt] = false;
  90. visit[2 * cnt] = false;
  91. visit[2 * cnt + 1] = false;
  92. visit[2 * cnt + 2] = false;
  93. }
  94.  
  95. return;
  96. }
  97.  
  98. int main(void) {
  99. ios_base::sync_with_stdio(false);
  100. cin.tie(nullptr);
  101.  
  102. cin >> n >> s;
  103. total = n / 2;
  104.  
  105. go(0);
  106. cout << ans << '\n';
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement