 When a scientist wants to analyze a DNA molecule, they perform a procedure called restriction digest. It uses a set of enzymes which cut the molecule into a certain set of DNA fragments accordingly to certain rules. In this problem you will be given a DNA molecule, and your task will be to construct a set of enzymes which have to be applied to the molecule to produce a set of DNA fragments which satisfy the given restrictions.
DNA molecule
DNA molecule consists of two strands (chains) of nucleotides, called forward strand and reverse strand. There are only four types of nucleotides  'A' (Adenine), 'C' (Cytosine), 'G' (Guanine) and 'T' (Thymine); nucleotides A and T are complementary to each other, and so are C and G. The strands consist of N nucleotides each, and for each i between 0 and N1 the ith nucleotide of forward strand is complementary to the ith nucleotide of reverse strand. For example, if the forward strand is "ATGC", then the reverse strand will be "TACG". Thus, the forward strand alone defines the DNA in a unique way.
In this problem the DNA molecule of N nucleotides will be represented as a String dna, consisting of N+1 characters. First N characters will describe the forward strand of the molecule, one character per nucleotide. The last character will be '.' if the molecule is linear, and '+' if it is circular (the ends of the molecule are connected so that the first nucleotide follows the last nucleotide).
Enzymes
For the purposes of this problem, enzymes are entities which, when applied to a DNA molecule, can cut it into fragments accordingly to certain rules. Each enzyme has a code  a unique String identifier, and a recognition sequence  a String which defines enzyme's behavior. Enzymes' recognition sequences can contain only nucleotide characters ('A', 'C', 'G', 'T') and wildcards (will be explained later). Before introducing the formal rules of enzymes' behavior, let's have a look at several examples. We will assume that the DNA is linear (circular DNA case is similar and will be explained later).
An enzyme with code "AciI" has recognition sequence "CCGC". This enzyme cuts the DNA if its forward strand contains "CCGC" as a contiguous substring (this is called a forward match). Both forward and reverse strands of DNA are cut when a match is found, but generally they are cut in different places. The offsets of the cuts from the start of the match in the forward and the reverse strands are given by two numbers A and B, respectively; for this enzyme A = 1 and B = 3. The cuts done by this enzyme are shown on the image:
The more formal way to describe the cuts done by the enzyme in case of a forward match is as follows. Let L denote the length of recognition sequence of the enzyme, and A and B  the offsets of the cuts in forward and reverse strands, respectively. If a forward match is found at position P and includes nucleotides from P to (P+L1), the cut in the forward strand is done between nucleotides (P+A1) and (P+A), and the cut in the reverse strand is done between nucleotides (P+B1) and (P+B). A cut between nucleotides P and (P+1) is considered to be positioned at P.
The enzyme also cuts the DNA if its reverse strand contains the reverse of recognition sequence as a contiguous substring (this is called a reverse match). You can think of it as rotating the molecule 180 degrees and searching for recognition sequence in forward strand of rotated molecule. The same numbers A and B define the offsets of the cuts, but this time in the reverse and the forward strands, respectively. The indices of the nucleotides between which the cuts are done will be the same, if we reverse the indexation of the nucleotides in all DNA (as in rotated molecule); in direct indices the reverse match includes nucleotides from P to (PL+1), the cut in reverse strand is done between nucleotides (PA+1) and (PA), and the cut in forward strand is done between nucleotides (PB+1) and (PB).
Note that the values of A and B can be zero, negative or greater than L1; this means that the cuts are done outside of the matched part of the DNA or on its borders (for 0 or L values). However, for a cut to be done, all values of (P+A1), (P+A), (P+B1) and (P+B) (for forward matches) or (PA+1), (PA), (PB+1) and (PB) (for reverse matches) must be valid nucleotides indices, i.e., be between 0 and N1, inclusive (remember that we consider only linear DNA at the moment; this is different for circular DNA). Thus, for example, enzyme "LweI" has recognition sequence "GCATC", and values of A = 10 and B = 14. Forward and reverse matches for this enzyme will produce the following cuts (nucleotides marked by * can have any value, but must exist in the molecule, while nucleotides marked by . might not exist):
Another thing to note is that forward and reverse matches can produce cuts in the same locations; thus, for example, enzyme "EcoRI" has recognition sequence "GAATTC", and values of A = 1 and B = 5. The recognition sequence is complementary to its reverse, so the forward and reverse matches will include the same nucleotides, and the cuts will be done in the same places:
Wildcards
Recognition patterns can contain wildcards  characters which match any nucleotide from a certain set. The complete list of wildcards and corresponsing sets of matching nucleotides follows.
Character Accepted nucleotides
M A, C
R A, G
W A, T
S C, G
Y C, T
K G, T
V A, C, G
H A, C, T
D A, G, T
B C, G, T
X,N G, A, T, C
4cuts enzymes
Some enzymes do four cuts per match (both forward and reverse match) instead of two. Such enzymes are described with two pairs of offsets (A1, B1) and (A2, B2). Each pair defines two cuts, one in forward strand, and one in reverse strand, just like in the 2cuts enzymes. All cut positions must be valid for any cuts to be done. Thus, for example, enzyme "BdaI" has recognition sequence "TGANNNNNNTCA", A1 = 10, B1 = 12, A2 = 24, B2 = 22. Each forward match (and each reverse one as well) will result in the following cuts:
Circular DNA molecules
In circular molecules all nucleotide indices are calculated modulo N. This means that a match can stretch across nucleotides (N1) and 0, as well as to the left of 0 and/or to the right of (N1), and still be a valid match. For example, this is a valid forward match for "EcoRI" which starts from nucleotide (N3) and ends at nucleotide 2:
The cuts are also evaluated modulo N; a cut between 2 and 1 is the same as between (N2) and (N1), and between (2*N2) and (2*N1). This means that all cuts are valid in circular molecule. The cut between nucleotides (N1) and 0 is considered to be positioned at (N1).
Applying a set of enzymes
Applying a given set of enzymes to a given DNA molecule is done as follows. First, for each enzyme from the set, all forward/reverse matches are found, and corresponding cut positions in forward/reverse strands are evaluated (but not performed yet!). After this, all cuts found are simultaneously applied to the DNA; it is possible to have several cuts at the same position, which is equivalent to a single cut at it.
Input Data
You will be given the following information:

String dna, describing the given DNA molecule.

String[] enzymes, describing available types of enzymes. Each element of enzymes will represent a single enzyme and will be formatted as "code seq A B" for 2cuts enzymes or "code seq A1 B1 A2 B2" for 4cuts enzymes (here code is enzyme's identifier and seq is its recognition sequence). The contents of enzymes will be the same over all test cases; you can download it here.

String restrictions, setting restrictions on the task you have to complete. It will always contain 8 elements; the formatting will be explained later.
The Task
You have to construct a set of enzymes S which will be applied to the given DNA. Once all cuts are done, each strand of DNA will be separated into several fragments; we are interested in fragments of forward strand only. Let K denote the number of fragments, and L_{0}, L_{1}, ..., L_{K1}  the lengths of the fragments sorted in nonascending order (i.e., L_{i} ≥ L_{i+1} for all i). The set of enzymes you construct must satisfy all conditions given in restrictions. Here they are:

restrictions[0] is "minFragmentCountt" (integer), specifying the minimal number of fragments: K ≥ minFragmentCount.

restrictions[1] is "maxFragmentCount" (integer), specifying the maximal number of fragments: K ≤ maxFragmentCount.

restrictions[2] is "minFragmentLength" (integer), specifying the minimal length of a fragment: L_{K1} ≥ minFragmentLength.

restrictions[3] is "maxFragmentLength" (integer), specifying the maximal length of a fragment: L_{0} ≤ maxFragmentLength.

restrictions[4] is "minFragmentDiff" (floatingpoint between 0 and 10, inclusive, with exactly 3 digits after decimal point), specifying a restriction on the difference of fragment lengths: 100 * (L_{i}  L_{i+1}) ≥ minFragmentDiff * L_{i}, for each i, 0 ≤ i ≤ K2.

restrictions[5] is "minRSLength" (integer), specifying the minimal length of the recognition sequence of each enzyme from set S.

restrictions[6] is "maxEnzymes" (integer), specifying the maximal number of enzymes in set S.

restrictions[7] specifies the ranges of positions where at least one cut must be done, and is interpreted as follows. If it is "none", the restriction is simply ignored. Otherwise it is a commaseparated list of pairs "left right", where all lefts and rights are integers, and each pair means that there must be at least one cut positioned from left to right, inclusive. For linear DNA, each pair will satisfy condition 0 ≤ left ≤ right ≤ N2. For circular DNA, each pair will satisfy condition 0 ≤ left, right ≤ N1; if left > right, the pair specifies cut positions left, left+1, ..., N1, 0, 1, ..., right.
Finally, the set S of enzymes must be irreducible: for any set S' obtained from S by removing one enzyme, the sets of cuts (in the forward strand) produced in this DNA by sets S and S' must be different.
Return value
Your program must find and return as many distinct sets S which satisfy the given restrictions as possible. If you can find more than a hundred of such sets, return any 100 of them. The return value is a String[], in which each element describes one set as a spaceseparated list of 0based enzyme indices in enzymes array, sorted in strictly ascending order (without duplicates), written without extra leading zeroes.
Scoring
The individual score for a test case is calculated as follows. Invalid return of any kind results in 0 score. If the return is valid, the score will be X + T/100.0, where X is the number of sets S you have found, and T is the processor time used by your program (in seconds).
Your overall score will be calculated as sum of normalized scores over all test cases. Normalized score for a valid return is given with the following formula
where X_{max} is the maximal number of sets found by any competitor for this test case. There are two exceptions from the general formula. If X_{max} is 0, then all competitors get 0 normalized score for this test case. If X is 0, the corresponding competitor gets 0 normalized score for this test case.
Test Case Generation
The test cases will be generated as follows. enzymes is a constant array taken from this file.
dna is either taken from a real molecula, or generated using Markov chain model. DNA length is chosen uniformly between 5000 and 100000, inclusive. 11 samples of real DNA molecules were analyzed to produce probabilities of all possible sequences of 1..4 nucleotides appearing in the molecula. The resulted distributions can be downloaded here.
When generating a DNA, the distribution number is first chosen. It will be distribution 11 with probability 1/3 and one of distributions 110 with probability 1/15 each. Then dna is constructed character by character based on the chosen distribution. At east step, last 3 characters are considered (let them form a string prev) and the next character is chosen as next with probability proportional to the number of occurrences of nucleotide sequence prev+next in the chosen distribution. The first 3 characters are generated based on previous 0, 1 and 2 characters, correspondingly. The last character of dna, which specifies whether it is linear or circular, is chosen as '.' or '+' with equal probabilities.
restrictions are generated in a rather complicated manner.

minFragmentCount and maxFragmentCount are chosen uniformly between 3 and 15, inclusive, so that maxFragmentCount ≥ minFragmentCount.

minFragmentLength, maxFragmentLength and minFragmentDiff are chosen together in the following way. minFragmentLength is chosen uniformly between 1 and N / minFragmentCount, inclusive. maxFragmentLength is chosen as ceil(N / maxFragmentCount) / x, where x is chosen uniformly between 0 and 1, so that maxFragmentLength ≤ N. If after this minFragmentLength > maxFragmentLength, generation of these three parameters restarts. Otherwise minFragmentDiff is chosen uniformly between 0 (inclusive) and 10 (exclusive), and it is verified that there exists a sequence of fragment lengths which satisfies the first 5 restrictions for values minFragmentCount, maxFragmentCount, minFragmentLength, maxFragmentLength, 10*minFragmentDiff (note that each sequence that satisfies the 5th restriction (1based) for 10*minFragmentDiff will also satisfy it for minFragmentDiff, but not vice versa); if it doesn't, generation restarts. Note that verification is done only for fragment lengths, it is not verified that there exists a set of enzymes that would produce exactly these fragment lengths.

minRSLength is chosen uniformly between minRS and (maxRS+minRS)/2, inclusive, where minRS and maxRS are minimal and maximal length of recognition sequence of all enzymes present in the input.

maxEnzymes is chosen uniformly between maxFragmentCount/2 and maxFragmentCount, inclusive.

ranges are present with probability 1/2; if they are, the number of ranges in the restriction is chosen uniformly between 1 and 3, inclusive, each pair is gerenated independently, and left and right of each range are chosen uniformly so that they form a valid range (see definition of ranges).
Tester
A tester is available for offline testing. You can check its source code for a precise implementation of test case generation and scoring. Note that it provides only test cases with generated DNA molecules.
Prizes and conditions
In order to receive the prize money, you will need to provide an explanation of how your solution works. Note that it should not be submitted anywhere during the coding phase. Instead, if you win a prize, a TopCoder representative will contact you directly in order to collect it. 