DNA Sequences
Time limit1sMemory limit128 MB
Given a DNA pattern with wildcards and a rank R, find the R-th lexicographic matching string that decomposes into at most K non-decreasing runs.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
One interesting task that computers help with is analyzing biological data such as DNA sequences. A single strand of DNA is a chain of the nucleotides adenine, cytosine, guanine, and thymine, denoted A, C, G, and T, respectively. A strand of DNA can therefore be written as a string over these four letters, and such a string is called a DNA sequence.
Sometimes the nucleotide at a given position on a strand cannot be determined exactly. In that case the unknown nucleotide is written with the letter N; that is, N is a wildcard that stands for any one of A, C, G, or T. A sequence that contains at least one N is called an incomplete sequence, and a sequence with no N is called a complete sequence. If each N in an incomplete sequence can be replaced by one of the four nucleotides to produce a given complete sequence, then that complete sequence is said to match the incomplete sequence. For example, ACCCT matches ACNNT, but AGGAT does not.
The four nucleotides are ordered alphabetically: A < C < G < T. A sequence is classified as type-1 if every nucleotide is equal to, or comes before, the nucleotide immediately to its right (that is, the sequence is non-decreasing from left to right). For example, AACCGT is type-1, but AACGTC is not.
In general, a type- sequence is defined as follows: a sequence is type- if it is type-, or if it is the concatenation of some type- sequence and some type-1 sequence. For example, AACCC, ACACC, and ACACA are type-3, but GCACAC and ACACACA are not.
Sequences are ordered lexicographically, the same way words are ordered in a dictionary. Thus, among the type-3 sequences of length 5, the first is AAAAA and the last is TTTTT. As another example, the first seven type-3 sequences that match the incomplete sequence ACANNCNNG, in order, are:
ACAAACAAG, ACAAACACG, ACAAACAGG, ACAAACCAG, ACAAACCCG, ACAAACCGG, ACAAACCTG
Given an incomplete sequence of length , write a program that finds the -th type- sequence, in lexicographic order, that matches it.
Input
The first line contains three integers , , and separated by single spaces. (, , )
The second line contains the incomplete sequence, a string of length consisting only of the characters A, C, G, T, and N.
The number of type- sequences that match the given incomplete sequence is at most , so it fits in a signed 64-bit integer (for example, long long in C/C++). is at most this number.
Output
Print, on the first line, the -th type- sequence in lexicographic order that matches the given incomplete sequence.