Semigroups

Time limit1sMemory limit128 MB

Summary
Given a small multiplication table, decide whether it defines a semigroup, report the first counterexample if not, and check commutativity.
Level

Easy3 of 10

Topics
Brute force, Implementation, Math, Simulation
Solved
No attempts yet

Problem

A binary operation on a set SS is a function that assigns to each ordered pair of elements of SS a unique element of SS. We usually use a special symbol (such as *, +, or #) to represent a binary operation. For example, if we use the symbol # for some binary operation on the set S={a,b,c}S = \{a, b, c\}, then a#b equals some element of SS, and so do b#a, a#a, a#c, and every other possible ordered pair.

From this definition, ordinary addition, subtraction, and multiplication are all binary operations on the set of all integers. However, division (the mathematical kind, not integer division) is not a binary operation on the integers, since 1/2 = 0.5 is not an integer.

The word "ordered" in the definition matters, because it allows the element assigned to a#b to differ from the one assigned to b#a. Integer subtraction is an example, since 5-3 is not equal to 3-5. If x#y = y#x holds for all elements xx and yy of the set, we say the binary operation is commutative. Ordinary addition on the integers is commutative.

For the rest of this problem we consider only small sets (1 to 26 elements). For such small sets, the assignments that define an operation can be written out fully as a "multiplication table". For instance, the binary operation # on S={a,b,c}S = \{a, b, c\} might be defined by:

#  |  a  b  c
-------------
a  |  b  c  b
b  |  a  c  b
c  |  c  b  a

The left column gives the first element of the ordered pair and the top row gives the second. So in this example a#b = c, b#a = a, and c#c = a. The body of the table must consist solely of elements of SS, which must hold for any binary operation. This operation is not commutative, since b#a is not equal to a#b.

A binary operation # on a set SS is associative if (x#y)#z = x#(y#z) for all elements xx, yy, and zz of the set. In the table above the operation is not associative, since (a#b)#c is not equal to a#(b#c). If a binary operation # is associative, then the pair ⟨S,#⟩\langle S, \# \rangle forms a semigroup. If the operation is both associative and commutative, the semigroup is a commutative semigroup.

Input

Read the elements of a set together with the "multiplication table" that defines a binary operation, and decide whether the set SS with that operation forms a semigroup. If it does not, report that it is not a semigroup and state why. If it does form a semigroup, also check whether the semigroup is commutative.

Thus, for each set and table, exactly one of the following four results applies:

NOT A SEMIGROUP: x#y = z  WHICH IS NOT AN ELEMENT OF THE SET
NOT A SEMIGROUP: (x#y)#z IS NOT EQUAL TO x#(y#z)
SEMIGROUP BUT NOT COMMUTATIVE  (x#y IS NOT EQUAL TO y#x)
COMMUTATIVE SEMIGROUP

In the first three results, substitute the actual elements of the set that form a counter-example to the definition. When more than one counter-example exists, report the first one found by scanning the table top to bottom (row major) and left to right within each row; for an associativity failure, report the first triple found when iterating xx, then yy, then zz in the set's given order.

The first line contains an integer nn (1≤n≤261 \le n \le 26).

The next line contains nn distinct lowercase letters. These are the elements of the set; they are all different but not necessarily in alphabetical order.

The next nn lines contain the body of the multiplication table for those elements. Each line contains nn lowercase letters; the first such line is the first row of the table body. The order of the rows and columns matches the order of the elements on the line that defines the set.

After the table, a line contains an integer nn (0≤n≤260 \le n \le 26). If n>0n > 0, another set and table follow in the next n+1n+1 lines and must also be processed. If n=0n = 0, the input has ended.

Output

For each set and table in the input, output the following:

  1. The elements of SS in the same order as the input, in this format: S = {a,b,c,d}
  2. A line that starts with one space, then the characters #|, then the nn elements of the set (no spaces or commas). Example (with a leading space): #|abcd
  3. A line that starts with one space, then the characters -+, then nn dashes -. Example (with a leading space): -+----
  4. The nn rows of the multiplication table in the same order as the input. The ii-th line starts with one space, then the ii-th element of the set, then |, then the nn characters of the ii-th table row (no spaces). Example (with a leading space): a|abcd
  5. One blank line.
  6. One line reporting the result, which must be one of the four possibilities above.
  7. A line of 30 dashes.
  8. One blank line separating this report from the following report.

Examples2

  1. Example 1

    Input
    3
    abc
    abc
    bca
    cab
    3
    abc
    abc
    bca
    cad
    4
    acdb
    aaaa
    aaca
    aada
    aaab
    5
    abcde
    aaaaa
    bbabb
    cccbc
    ddddd
    eeeee
    0
    
    Expected output
    S = {a,b,c}
     #|abc
     -+---
     a|abc
     b|bca
     c|cab
    
    COMMUTATIVE SEMIGROUP
    ------------------------------
    
    S = {a,b,c}
     #|abc
     -+---
     a|abc
     b|bca
     c|cad
    
    NOT A SEMIGROUP: c#c = d  WHICH IS NOT AN ELEMENT OF THE SET
    ------------------------------
    
    S = {a,c,d,b}
     #|acdb
     -+----
     a|aaaa
     c|aaca
     d|aada
     b|aaab
    
    SEMIGROUP BUT NOT COMMUTATIVE  (c#d IS NOT EQUAL TO d#c)
    ------------------------------
    
    S = {a,b,c,d,e}
     #|abcde
     -+-----
     a|aaaaa
     b|bbabb
     c|cccbc
     d|ddddd
     e|eeeee
    
    NOT A SEMIGROUP: (b#a)#c IS NOT EQUAL TO b#(a#c)
    ------------------------------
    
  2. Example 2

    Input
    1
    a
    a
    0
    
    Expected output
    S = {a}
     #|a
     -+-
     a|a
    
    COMMUTATIVE SEMIGROUP
    ------------------------------