-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1512C.cpp
More file actions
executable file
·64 lines (54 loc) · 1.01 KB
/
Copy path1512C.cpp
File metadata and controls
executable file
·64 lines (54 loc) · 1.01 KB
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
/*
Problem Code: 1512C
Time: O(n)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool is_palindrome(int n, string& s) {
return equal(s.begin(), s.begin() + n / 2, s.rbegin());
}
string ab_palindrome(int a, int b, string& s) {
int n = s.length();
for (int i = 0; i < n; i++)
if (s[i] == '?')
s[i] = s[n - i - 1];
a -= count(s.begin(), s.end(), '0');
b -= count(s.begin(), s.end(), '1');
if ((n & 1) && s[n / 2] == '?') {
if (a & 1)
a--, s[n / 2] = '0';
else
b--, s[n / 2] = '1';
}
for (int i = 0; i < n / 2; i++) {
if (s[i] == '?') {
if (a) {
a -= 2;
s[i] = s[n - i - 1] = '0';
} else {
b -= 2;
s[i] = s[n - i - 1] = '1';
}
}
}
if (a || b || !is_palindrome(n, s))
return "-1";
else
return s;
}
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
string s;
cin >> a >> b >> s;
cout << ab_palindrome(a, b, s) << endl;
}
return 0;
}