Advertisement
Guest User

Untitled

a guest
Apr 11th, 2021
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7.     int t;
  8.     cin >> t;
  9.     while (t--)
  10.     {
  11.         int a, b;
  12.         cin >> a >> b;
  13.         string s;
  14.         cin >> s;
  15.         //number of zeroes in the string
  16.         int count_z = 0;
  17.         //number of ones in the string
  18.         int count_o = 0;
  19.         //number of ?(s) in the string
  20.         int count_q = 0;
  21.         int n = a + b;
  22.         bool p = true;
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.             if (s[i] == '0')
  26.             {
  27.                 count_z++;
  28.             }
  29.             if (s[i] == '1')
  30.             {
  31.                 count_o++;
  32.             }
  33.             if (s[i] == '?')
  34.             {
  35.                 count_q++;
  36.             }
  37.         }
  38.         for (int i = 0; i < n; i++)
  39.         {
  40.             // get the element at n - i - 1 position
  41.             int trans;
  42.             if ((n - i - 1) >= 0)
  43.             {
  44.                 trans = n - i - 1;
  45.             }
  46.             else
  47.             {
  48.                 trans = 0;
  49.             }
  50.             if (s[i] == '?' && s[ n - i- 1] == '?')
  51.             {
  52.                 if (i != trans)
  53.                 {
  54.                     if (count_o < b)
  55.                     {
  56.                         s[i] = '1';
  57.                         s[trans] = '1';
  58.                         count_o = count_o + 2;
  59.                         count_q = count_q - 2;
  60.                     }
  61.                     else if (count_z < a)
  62.                     {
  63.                         s[i] = '0';
  64.                         s[trans] = '0';
  65.                         count_z = count_z + 2;
  66.                         count_q = count_q - 2;
  67.                     }
  68.                 }
  69.                 else
  70.                 {
  71.                     if (count_o < b)
  72.                     {
  73.                         s[i] = '1';
  74.                         count_o++;
  75.                         count_q--;
  76.                     }
  77.                     else if (count_z < a)
  78.                     {
  79.                         s[i] = '0';
  80.                         count_z++;
  81.                         count_q--;
  82.                     }
  83.                 }
  84.             }
  85.             else if (s[i] == '?' && s[trans] != '?')
  86.             {
  87.                 s[i] = s[trans];
  88.                 if (s[trans] == '0')
  89.                 {
  90.                     count_z++;
  91.                 }
  92.                 else
  93.                 {
  94.                     count_o++;
  95.                 }
  96.                 count_q--;
  97.             }
  98.             else if (s[i] != '?' && s[trans] == '?')
  99.             {
  100.                 s[trans] = s[i];
  101.                 if (s[i] == '0')
  102.                 {
  103.                     count_z++;
  104.                 }
  105.                 else
  106.                 {
  107.                     count_o++;
  108.                 }
  109.                 count_q--;
  110.             }
  111.             else if (s[i] != '?' && s[trans] != '?' && s[i] != s[trans])
  112.             {
  113.                 cout << -1;
  114.                 p = false;
  115.                 break;
  116.             }
  117.         }
  118.         if (!(count_z == a && count_o == b))
  119.         {
  120.             cout << -1;
  121.         }
  122.         if (count_z == a && count_o == b && p)
  123.         {
  124.             for (int i = 0; i < n; i++)
  125.             {
  126.                 cout << s[i];
  127.             }
  128.         }
  129.         cout << endl;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement