fork download
  1. // Problem Link: https://c...content-available-to-author-only...s.com/contest/1512/problem/C
  2.  
  3. #include <bits/stdc++.h>
  4. #define INF 1e18
  5. #define EPS 1e-9
  6. #define PI 3.1415926535897932384626433832795
  7. #define MOD 1000000007
  8. #define endl "\n";
  9. #define _min(a) *min_element(a.begin(), a.end());
  10. #define _max(a) *max_element(a.begin(), a.end());
  11. #define _sort(x) sort(x.begin(), x.end())
  12. #define _rsort(x) sort(x.rbegin(), x.rend())
  13. #define _reverse(x) reverse(x.begin(), x.end())
  14. #define _debug(x) cout << #x << " = " << x << endl;
  15. #define debug(x, y) cout << #x " = " << x << " " << #y << " = " << y << endl;
  16. #define _lower_bound(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
  17. #define _upper_bound(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
  18. typedef long long ll;
  19. using namespace std;
  20.  
  21. vector<int> read_array(int n) {vector<int>a(n); for(int i=0; i<n;i++) cin>>a[i]; return a;}
  22. vector<ll> read_array(ll n) {vector<ll>a(n); for(int i=0; i<n;i++) cin>>a[i]; return a;}
  23. vector<pair<int,int>> read_pair(int n) {vector<pair<int, int>>a(n); for(int i=0; i<n; i++) {cin>>a[i].first>>a[i].second;} return a;}
  24. void write_array(vector<int>a) {for(auto &u:a) cout<<u<< " "; cout<<endl;}
  25.  
  26. void solve() {
  27. int a, b; cin >> a >> b;
  28. string s; cin >> s;
  29. int n = s.length();
  30. if (n != a+b) {
  31. cout << "-1\n";
  32. return;
  33. }
  34. if (a % 2 == 1 && b % 2 == 1) {
  35. // puts("WARNING: 1");
  36. cout << "-1\n";
  37. return;
  38. }
  39. if (a % 2 == 1 && s[n/2] == '?') {
  40. s[n/2] = '0';
  41. // a--;
  42. }
  43. if (b % 2 == 1 && s[n/2] == '?') {
  44. s[n/2] = '1';
  45. // b--;
  46. }
  47. for (int i = 0; i < n; i++) {
  48. if (s[i] == '0') a--;
  49. if (s[i] == '1') b--;
  50. }
  51. if (a < 0 || b < 0) {
  52. // cout << a << b << endl;
  53. // puts("WARNING: 2");
  54. cout << "-1\n";
  55. return;
  56. }
  57. int i = 0, j = n - 1;
  58. while (i < j) {
  59. if (a < 0 || b < 0) {
  60. // puts("WARNING: 3");
  61. cout << "-1\n";
  62. return;
  63. }
  64. if (s[i] == '?' && s[j] != '?') {
  65. if (s[j] == '0') {
  66. s[i] = '0';
  67. a--;
  68. } else {
  69. s[i] = '1';
  70. b--;
  71. }
  72. } else if (s[i] != '?' && s[j] == '?') {
  73. if (s[i] == '0') {
  74. s[j] = '0';
  75. a--;
  76. } else {
  77. s[j] = '1';
  78. b--;
  79. }
  80. } else if (s[i] != '?' && s[j] != '?') {
  81. if (s[i] != s[j]) {
  82. // puts("WARNING: 4");
  83. cout << "-1\n";
  84. return;
  85. } else {
  86.  
  87. }
  88. } else {
  89. if (a >= b) {
  90. s[i] = s[j] = '0';
  91. a -= 2;
  92. } else {
  93. s[i] = s[j] = '1';
  94. b -= 2;
  95. }
  96. }
  97.  
  98. i++;
  99. j--;
  100. }
  101. if (a < 0 || b < 0) {
  102. cout << "-1\n";
  103. return;
  104. }
  105. i = 0, j = n - 1;
  106. while (i < j) {
  107. if (s[i] != s[j]) {
  108. cout << "-1\n";
  109. return;
  110. }
  111. i++;
  112. j--;
  113. }
  114. cout << s << endl;
  115. }
  116.  
  117. int main() {
  118. ios_base::sync_with_stdio(0);
  119. cin.tie(0); cout.tie(0);
  120.  
  121. int t; cin >> t;
  122. while(t--) {
  123. solve();
  124. }
  125. }
Success #stdin #stdout 0.01s 5384KB
stdin
Standard input is empty
stdout
Standard output is empty