Molecules |
In this abstraction from a molecular engineering problem associated with developing a synthetic fuel, we are given four, equal-length, molecular chains that are to form a super molecule. In the simplified two-dimensional model used here, the super molecule is formed as an interlocking rectangular arrangement of the four given molecular chain strands. The interlocking feature is the sharing of a common molecule between pairs of chains.
To illustrate, suppose we have the four, length-twelve, molecular chains:
O I M D I H E I A F N L C H J D B J M H P J K D L C B J O J G I E K B O K A I N L H L O L B E J
These can be placed in the interlocking arrangements:
O L I C M B C H J D B J M H P J K D I O H J E G I I A E F K K A I N L H L O L B E J L O
-OR-
O C I H M J D D L C B J O J G I E K B O H J E M K A I N L H L O L B E J A P F J N K L D
In this problem, we have some constraints on the arrangements being sought:
The area is measured as the count of vacant character positions within the enclosed rectangle of the super molecule. The area counts of the two super molecules illustrated above are thirty (30) and four (4).
The input consists of a series of data sets. Each data set consists of four molecular chains of 12 fixed elements each. These 12 elements are given as contiguous capital letters. The molecule designators within the chains will be restricted to the sixteen letters, A...P. The first letter of a chain will appear as the first character on an input line.
The first molecule designator within the first chain of a data set will be the letter "Q" to indicate the end of data.
A line with a single integer is to be emitted for each input data set encountered. This integer is the maximum area enclosed by any legitimate arrangement of the four chains.
Use the output value zero (0) to indicate that no legitimate super molecule could be formed for a givendata set.
The first digit of an output value should be the first character on a line.
C D B A D C B B E F E F D A C C B A D A F E A B E F B D C A A D B D C D A B C D A B C D A B C D D A C C B A D A F E A B E F B D C A A D B D C D A B C D A B C D A B C D C D B A D C B B E F E F A B A B A B A B A B A B C D C D C D C D C D C D E E E E E E E E E E E E F F F F F F F F F F F F A B A A A A A A A A B A C B C C C C C C C C B C D B D D D D D D D D B D E B E E E E E E E E B E A B B B B B B B B B B A A C C C C C C C C C C A A D D D D D D D D D D A A E E E E E E E E E E A B B B A B B B A B B B B C C A C C C A C C C C C D D D D A D D A D D D D E E A E E A E E E E E E Q
48 48 0 64 0 6