-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1512_C_A-B_Palindrome.cpp
More file actions
111 lines (107 loc) · 2.5 KB
/
1512_C_A-B_Palindrome.cpp
File metadata and controls
111 lines (107 loc) · 2.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int a, b;
string str;
cin >> a >> b;
cin >> str;
int x, y;
int n = str.size();
x = 0;
y = 0;
for(int i=0; i<n; i++){
if(str[i] == '1'){
y++;
}
else if(str[i] == '0'){
x++;
}
}
a -= x;
b -= y;
if(a < 0 || b < 0){
cout << -1 << endl;
continue;
}
if(x+y == str.size()){
string temp = str;
reverse(temp.begin(), temp.end());
if(temp == str){
cout << str << endl;
}
else{
cout << -1 << endl;
}
continue;
}
for(int i=0; i<n/2; i++){
if(str[i] == '?' && str[n-i-1] != '?'){
if(str[n-i-1] == '0'){
a--;
str[i] = '0';
}
else{
b--;
str[i] = '1';
}
}
else if(str[i] != '?' && str[n-i-1] == '?'){
if(str[i] == '0'){
a--;
str[n-i-1] = '0';
}
else{
b--;
str[n-i-1] = '1';
}
}
}
int check = 0;
for(int i=0; i<n/2; i++){
if(str[i] == '?' && str[n-i-1] == '?'){
if(a >= 2){
str[i] = '0';
str[n-i-1] = '0';
a -= 2;
}
else if(b >= 0){
str[i] = '1';
str[n-i-1] = '1';
b -= 2;
}
else{
check = 1;
break;
}
}
}
if(check == 1){
cout << -1 << endl;
continue;
}
if(str[n/2] == '?'){
if(a>0){
str[n/2] = '0';
}
else{
str[n/2] = '1';
}
}
if(a != 0 && b != 0){
cout << -1 << endl;
continue;
}
string temp = str;
reverse(temp.begin(), temp.end());
if(temp == str){
cout << str << endl;
}
else{
cout << -1 << endl;
}
}
return 0;
}