-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddPlusMinuSign.cpp
67 lines (61 loc) · 1.77 KB
/
AddPlusMinuSign.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Problem statement:
AquaMoon has a string a consisting of only 0 and 1. She wants to add + and − between all pairs of consecutive positions to make the absolute value
of the resulting expression as small as possible. Can you help her?
Input
The first line contains a single integer t (1≤t≤2000) – the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (2≤n≤100) — the length of a.
The second line of each test case contains a string a of length n, consisting of only 0 and 1.
Output
For each test case, output a string of length n−1 consisting of − and + on a separate line. If there is more than one assignment of signs that produces the smallest possible absolute value, any of them is accepted.
*/
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--) {
string s;
int n;
vector<char> v;
cin >> n >> s;
int fir, sec;
if (s[0] == '0') {
fir = 0;
}
else {
fir = 1;
}
int temp = fir;
for (int i = 0; i < n-1; i++) {
if (s[i] == '0') {
fir = 0;
}
else {
fir = 1;
}
if (s[i+1] == '0') {
sec = 0;
}
else {
sec = 1;
}
if (temp >= sec) {
v.push_back('-');
temp = temp - sec;
}
else {
v.push_back('+');
temp = temp + sec;
}
}
for (int i = 0; i < v.size(); i++) {
cout << v[i];
}
cout << "\n";
}
return 0;
}