Dominoes#542

Solution

There are very few possibilities. Choose which of Andriy's two dominoes goes to the left and which goes to the right in two ways. Each domino has two orientations, so there are only cases.

For each case, check that:

  1. the right half of the left domino equals ;

  2. the left half of the right domino equals ;

  3. the new left endpoint matches neither half of Taras's domino;

  4. the new right endpoint also matches neither half of Taras's domino.

Print the first case that satisfies all conditions. If none does, print .

Complexity

A constant number of cases is checked, so the time and extra memory are both .