|
| 1 | +/* |
| 2 | +Problem |
| 3 | +You are given a string SS of length NN, consisting of the digits 0-9 and the characters '+' and '-'. SS represents a valid mathematical expression. |
| 4 | +
|
| 5 | +Rearrange the characters of SS to form a valid mathematical expression such that the result obtained upon evaluating it is maximum. |
| 6 | +
|
| 7 | +If there are multiple possible answers, you may print any of them. |
| 8 | +
|
| 9 | +Note: A string SS of length NN is said to be a valid mathematical expression if the following hold: |
| 10 | +
|
| 11 | +The first character of SS is not + or -. |
| 12 | +The last character of SS is not + or -. |
| 13 | +Any + or - in SS must not be adjacent to another + or -. |
| 14 | +In particular, numbers are allowed to have leading zeros, and adding/subtracting zero is fine. |
| 15 | +
|
| 16 | +Input Format |
| 17 | +The first line of input will contain a single integer TT, denoting the number of test cases. |
| 18 | +Each test case consists of 22 lines of input. |
| 19 | +The first line of each test case contains a single integer NN, denoting the size of the string. |
| 20 | +The second line of each test case contains the string SS. |
| 21 | +Output Format |
| 22 | +For each test case, output on a new line the rearranged string giving the maximum value upon evaluation. If there are multiple possible answers, you may print any of them. |
| 23 | +
|
| 24 | +Constraints |
| 25 | +1 \leq T \leq 10001≤T≤1000 |
| 26 | +3 \leq N \leq 10^53≤N≤10 |
| 27 | +5 |
| 28 | + |
| 29 | +Each character of SS is one of \{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -\}{0,1,2,3,4,5,6,7,8,9,+,−}. |
| 30 | +The sum of N across all test cases won't exceed 1.5 \times 10^61.5×10 |
| 31 | +6 |
| 32 | + . |
| 33 | +Sample 1: |
| 34 | +Input |
| 35 | +Output |
| 36 | +3 |
| 37 | +7 |
| 38 | +4-89+20 |
| 39 | +5 |
| 40 | +5-9+0 |
| 41 | +3 |
| 42 | +9-5 |
| 43 | +984+2-0 |
| 44 | +5+9-0 |
| 45 | +9-5 |
| 46 | +Explanation: |
| 47 | +Test case 11: The given output expression evaluates to 986986, which is the maximum possible. |
| 48 | +
|
| 49 | +Test case 22: The given output expression evaluates to 1414, which is the maximum possible. |
| 50 | +
|
| 51 | +Test case 33: The given output expression evaluates to 44, which is the maximum possible. |
| 52 | +*/ |
| 53 | + |
| 54 | +#include <bits/stdc++.h> |
| 55 | +#include <bits/stdc++.h> |
| 56 | + |
| 57 | +using namespace std; |
| 58 | + |
| 59 | +int main() { |
| 60 | + int talab; |
| 61 | + cin>>talab; |
| 62 | + while(talab--){ |
| 63 | + int f; |
| 64 | + cin>>f; |
| 65 | + string s; |
| 66 | + cin>>s; |
| 67 | + int plus = 0, minus = 0, dig = 0; |
| 68 | + int d[10] = {0}; |
| 69 | + for (int i = 0; i < f; i++) { |
| 70 | + if(s[i] == '+'){ |
| 71 | + plus++; |
| 72 | + } |
| 73 | + else if(s[i]=='-'){ |
| 74 | + minus++; |
| 75 | + } |
| 76 | + else{ |
| 77 | + d[s[i]-'0']++; |
| 78 | + dig++; |
| 79 | + } |
| 80 | + } |
| 81 | + string ans = ""; |
| 82 | + int i = 9; |
| 83 | + while(dig-(plus+minus)){ |
| 84 | + if(d[i] == 0){ |
| 85 | + i--; |
| 86 | + continue; |
| 87 | + } |
| 88 | + ans += ('0'+i); |
| 89 | + d[i]--; |
| 90 | + dig--; |
| 91 | + } |
| 92 | + while(plus){ |
| 93 | + if(d[i] == 0){ |
| 94 | + i--; |
| 95 | + continue; |
| 96 | + } |
| 97 | + ans += '+'; |
| 98 | + ans += ('0'+i); |
| 99 | + d[i]--; |
| 100 | + plus--; |
| 101 | + } |
| 102 | + while(minus){ |
| 103 | + if(d[i] == 0){ |
| 104 | + i--; |
| 105 | + continue; |
| 106 | + } |
| 107 | + ans += '-'; |
| 108 | + ans += ('0'+i); |
| 109 | + d[i]--; |
| 110 | + minus--; |
| 111 | + } |
| 112 | + cout<<ans; |
| 113 | + cout<<"\n"; |
| 114 | + } |
| 115 | + return 0; |
| 116 | +} |
0 commit comments