fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define mod 1e9+7
  5. #define pb push_back
  6.  
  7.  
  8. bool check(string s, ll n)
  9. {
  10.  
  11. // cout<<n<<"\n";
  12. ll ptr1=0 , ptr2 = n-1;
  13.  
  14. while(ptr1<=ptr2)
  15. {
  16. if(s[ptr1]!=s[ptr2])
  17. return 0;
  18.  
  19. ptr1++;
  20. ptr2--;
  21.  
  22. }
  23.  
  24. return 1;
  25. }
  26.  
  27.  
  28. void solve()
  29. {
  30. int a,b;
  31. cin>>a>>b;
  32.  
  33. string s;cin>>s;
  34.  
  35. ll n = s.size();
  36.  
  37. ll cnt0=0,cnt1=0;
  38.  
  39. for(ll i=0;i<n;i++)
  40. {
  41. if(s[i]=='0')
  42. cnt0++;
  43. if(s[i]=='1')
  44. cnt1++;
  45. }
  46.  
  47. int aa=a;
  48. int bb=b;
  49.  
  50. a-=cnt0;
  51. b-=cnt1;
  52.  
  53.  
  54.  
  55. ll ptr1=0,ptr2=n-1;
  56.  
  57. while(ptr1<=ptr2)
  58. {
  59. // cout<<s<<"\n";
  60. // cout<<ptr1<<" "<<ptr2<<"\n";
  61.  
  62. if(s[ptr1]=='?' && s[ptr2]=='?')
  63. {
  64. if(a>b)
  65. {
  66. s[ptr1]='0';
  67. s[ptr2]='0';
  68. ptr1==ptr2 ? a-=1 : a-=2;
  69.  
  70. ptr1++;
  71. ptr2--;
  72. continue;
  73. }
  74. if(b>a)
  75. {
  76. s[ptr1]='1';
  77. s[ptr2]='1';
  78. ptr1==ptr2 ? b-=1 : b-=2;
  79.  
  80. ptr1++;
  81. ptr2--;
  82.  
  83. continue;
  84. }
  85.  
  86. s[ptr1]='1';
  87. s[ptr2]='1';
  88. ptr1==ptr2 ? b-=1 : b-=2;
  89.  
  90. ptr1++;
  91. ptr2--;
  92.  
  93. continue;
  94. }
  95.  
  96. if(s[ptr1]=='?' && s[ptr2] == '1')
  97. {
  98. s[ptr1] = '1';
  99. ptr1++;
  100. ptr2--;
  101. b-=1;
  102. continue;
  103. }
  104.  
  105. if(s[ptr1]=='?' && s[ptr2] == '0')
  106. {
  107. s[ptr1] = '0';
  108. ptr1++;
  109. ptr2--;
  110. a-=1;
  111. continue;
  112. }
  113.  
  114. if(s[ptr1]=='1' && s[ptr2] == '?')
  115. {
  116. s[ptr2] = '1';
  117. // cout<<s[ptr2];
  118. ptr1++;
  119. ptr2--;
  120. b-=1;
  121. continue;
  122. }
  123.  
  124. if(s[ptr1]=='0' && s[ptr2] == '?')
  125. {
  126. s[ptr2] = '0';
  127. ptr1++;
  128. ptr2--;
  129. a-=1;
  130. continue;
  131. }
  132.  
  133. ptr1++;
  134. ptr2--;
  135. }
  136.  
  137. // cout<<a<<" "<<b<<"\n";
  138.  
  139. if(a<0 || b<0){
  140. cout<<"-1\n";
  141. return ;
  142. }
  143.  
  144. ll mn = aa+bb;
  145.  
  146. if(check(s , mn))
  147. {
  148. cout<<s<<"\n";
  149. }
  150. else
  151. cout<<"-1\n";
  152.  
  153. // cout<<s;
  154. }
  155.  
  156. int main()
  157. {
  158. int t=1;
  159. cin>>t;
  160.  
  161. while(t--)
  162. {
  163. solve();
  164. }
  165. }
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
-1