A colony of alien bacteria has recently been discovered close to a crater in New Mexico. Dr. Poucher is in
charge of the scientific team at the ICPC BioLab committed to the study of the alien DNA structure. We
briefly sketch their discoveries here.
Alien DNA molecules have the structure of a circular sequence. Each sequence is composed of nucleotides.
There are 26 different types of nucleotides, and each of them can occur in two faces. It is very important to
remark that in any given alien DNA molecule, every nucleotide either does not appear at all or appears exactly
twice (hence, the length of a DNA molecule is an even integer between 2 and 52). In case a nucleotide occurs
twice, each occurrence can be of either type independently. Alien bacteria have two types of extremities,
which in the technical biological jargon are referred to as arms and legs. A major discovery of Dr. Poucher’s
team is a method to determine the exact number of arms and legs of a bacterium by examining its DNA
structure.
Here we represent each nucleotide as a letter of the alphabet. We refer to the different nucleotides as a,
A, ...z, Z, where the lowercase and uppercase forms of a letter represent the two possible faces a nucleotide
may appear with; we shall also use a/A, b/B, . . . z/Z to refer to a nucleotide in either face.
To determine the number of extremities, Dr. Poucher starts by initializing two counters of arms and legs
to zero, and then proceeds to perform a number of surgeries, transforming a DNA sequence into another
one. After each transformation, you may need to increase some of the counters, depending on the type of
surgery applied. When the empty sequence of nucleotides (which will be denoted by ∅) has been reached,
the number of extremities of the original molecule has been found. The possible surgeries are:
Second, if both occurrences of a/A are of the same face, one of the chains is “inverted” by reversing
the sequence and changing the face of every nucleotide in the chain.
Then, the chains are combined by concatenating the subsequence occurring before a with the sub-
sequence occurring after A, and the subsequence occurring after a with the sub-sequence occurring
before A.
Finally, two new a/A nucleotides are added to close the chain into a circular shape. The face of the new
nucleotides are the same if the original pair of nucleotides selected had the same face, and is different
otherwise.
Formally, suppose you select the nucleotide a/A, and further assume for the moment that it appears
both times with the face a (A). The cut and paste surgery turns sequences of the form S1 a S2 S3 a S4
(respectively S1 A S2 S3 A S4 ) into S2 a S1 BAR(S3) a BAR(S4) (respectively S2 A S1 BAR(S3) A BAR(S4) ). On the other hand, if nucleo-
tide a/A appears with its two different faces, the surgery turns sequences of the form S1 aS2 S3 AS4 into
S2 aS1 S4 AS3 . S1 , S2 , S3 and S4 are arbitrary sub-chains (possibly empty). In both cases the original
circular chain was chopped into S1 (a/A)S2 and S3 (a/A)S4 .
For example (see the figure below): starting with the sequence BacDcAbD, we can get chains BacDc and
AbD. Then, merging at nucleotide a/A we get the sequence cDca’BbDA’ where a’ and A’ represent the
new a/A nucleotides. Here, S1 = B, S2 = cDc, S3 = ∅ and S4 = bD.
Another example: take the same DNA sequence BacDcAbD, and cut to get the chains DBac and DcAb;
paste nucleotide c/C (in this case you need to reverse one chain, for example BaCd) to get the sequence
cDBadcBa. Here, S1 = DBa, S2 = ∅, S3 = D and S4 = Ab.
This surgery does not modify the number of arms or legs, but can be used cleverly in combination with
the previous surgeries to reduce the size of the DNA molecule and finish the calculation.
Each test case consists of a string of even length between 2 and 52, inclusive, representing the DNA
structure of an alien bacterium. All characters are letters. There will be one case per line in the input. The
last line contains the word “END” and must not be processed.
The output for each test case should have exactly one line, containing the number of arms or legs the
bacterium will have, followed by the word “arms” or “legs” respectively (if the number is 1, the words should
be in singular). In case there will be neither arms nor legs, the program should print the word “none”.
rkrk abcdeABCDE shcoOCfFHS END
1 arm 2 legs none
Migrated from old NTUJ.
SWERC 2009 pE
No. | Testdata Range | Score |
---|