Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main(void)
- {
- int t;
- cin >> t;
- while (t--)
- {
- int a, b;
- cin >> a >> b;
- string s;
- cin >> s;
- //number of zeroes in the string
- int count_z = 0;
- //number of ones in the string
- int count_o = 0;
- //number of ?(s) in the string
- int count_q = 0;
- int n = a + b;
- bool p = true;
- for (int i = 0; i < n; i++)
- {
- if (s[i] == '0')
- {
- count_z++;
- }
- if (s[i] == '1')
- {
- count_o++;
- }
- if (s[i] == '?')
- {
- count_q++;
- }
- }
- for (int i = 0; i < n; i++)
- {
- // get the element at n - i - 1 position
- int trans;
- if ((n - i - 1) >= 0)
- {
- trans = n - i - 1;
- }
- else
- {
- trans = 0;
- }
- if (s[i] == '?' && s[ n - i- 1] == '?')
- {
- if (i != trans)
- {
- if (count_o < b)
- {
- s[i] = '1';
- s[trans] = '1';
- count_o = count_o + 2;
- count_q = count_q - 2;
- }
- else if (count_z < a)
- {
- s[i] = '0';
- s[trans] = '0';
- count_z = count_z + 2;
- count_q = count_q - 2;
- }
- }
- else
- {
- if (count_o < b)
- {
- s[i] = '1';
- count_o++;
- count_q--;
- }
- else if (count_z < a)
- {
- s[i] = '0';
- count_z++;
- count_q--;
- }
- }
- }
- else if (s[i] == '?' && s[trans] != '?')
- {
- s[i] = s[trans];
- if (s[trans] == '0')
- {
- count_z++;
- }
- else
- {
- count_o++;
- }
- count_q--;
- }
- else if (s[i] != '?' && s[trans] == '?')
- {
- s[trans] = s[i];
- if (s[i] == '0')
- {
- count_z++;
- }
- else
- {
- count_o++;
- }
- count_q--;
- }
- else if (s[i] != '?' && s[trans] != '?' && s[i] != s[trans])
- {
- cout << -1;
- p = false;
- break;
- }
- }
- if (!(count_z == a && count_o == b))
- {
- cout << -1;
- }
- if (count_z == a && count_o == b && p)
- {
- for (int i = 0; i < n; i++)
- {
- cout << s[i];
- }
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement