hroc104-Editorial

Here is the Solution


<h2>int main()
{
   int T;
   cin >> T;
   while (T--)
   {
 
    string S;
 
    cin >> S;
 
    string S2;
    cin >> S2;
 
    int count1 = 0;
 
    int count0 = 0;
 
    int countq = 0;
 
    for (int i = 0; i < S.length(); i++)
    {
 
      if (S[i] == '0')
      {
 
        count0++;
 
      }
      else if (S[i] == '1')
      {
 
        count1++;
 
      }
      else
      {
 
        countq++;
 
      }
 
    }
 
    int c1 = 0;
    int c0 = 0;
 
    for (int i = 0; i < S2.length(); i++)
    {
 
      if (S2[i] == '0')
        c0++;
 
      else
        c1++;
 
    }
 
 
    if (c0 > count0 + countq)
    {
 
      cout << "-1\n";
 
      continue;
 
    }
 
 
int zeros = c0 - count0;
 
    if (zeros > 0)
    {
 
      int ct = 0;
 
      for (int i = 0; i < S.length(); i++)
      {
 
        if (S[i] == '?' and S2[i] == '0' and ct < zeros)
        {
 
          S[i] = '0';
 
          ct++;
 
        }
 
      }
 
      for (int i = 0; i < S.length(); i++)
      {
 
        if (S[i] == '?' and ct < zeros)
        {
 
          S[i] = '0';
 
          ct++;
 
        }
        else if (S[i] == '?')
        {
 
          S[i] = '1';
 
        }
 
      }
 
    }
   else
   {
 
      for (int i = 0; i < S.length(); i++)
      {
 
        if (S[i] == '?')
          S[i] = '1';
 
      }
 
   }
 
   int zo = 0, oz = 0;
 
   int ans = countq;
 
   for (int i = 0; i < S.length(); i++)
   {
 
      if (S[i] == '0' and S2[i] == '1')
        zo++;
 
      else if (S[i] == '1' and S2[i] == '0')
        oz++;
 
   }
 
   ans += oz + (zo - oz);
 
   cout << ans << endl;
 
  }
  return 0;
 
}

It was helpful

1 Like

Nice explanation…

1 Like