DNA Sequences

Time limit1sMemory limit128 MB

Summary
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-jj sequence is defined as follows: a sequence is type-jj if it is type-(j−1)(j-1), or if it is the concatenation of some type-(j−1)(j-1) 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 MM, write a program that finds the RR-th type-KK sequence, in lexicographic order, that matches it.

Input

The first line contains three integers MM, KK, and RR separated by single spaces. (1≤M≤500001 \le M \le 50000, 1≤K≤101 \le K \le 10, 1≤R≤2×10121 \le R \le 2 \times 10^{12})

The second line contains the incomplete sequence, a string of length MM consisting only of the characters A, C, G, T, and N.

The number of type-KK sequences that match the given incomplete sequence is at most 4×10184 \times 10^{18}, so it fits in a signed 64-bit integer (for example, long long in C/C++). RR is at most this number.

Output

Print, on the first line, the RR-th type-KK sequence in lexicographic order that matches the given incomplete sequence.

Examples2

  1. Example 1

    Input
    9 3 5
    ACANNCNNG
    
    Expected output
    ACAAACCCG
    
  2. Example 2

    Input
    5 4 10
    ACANN
    
    Expected output
    ACAGC