EOJ Monthly 2019.11 A (binary conversion)

"Welcome aboard Eastern Airlines flight MU5692 from the Yinchuan to Shanghai ......"

"Our aircraft will take off soon, please put away the tray table, off the headphones ......"

Put away the table board, took off the headphones, Cuber QQ suddenly doing nothing.

Time to put the headset into a bag, Cuber QQ inadvertently brought out some small piece of paper. They are memories of the past.

Note in the bag already exists not know how long. Water stains yellowed paper, some words he can not recognize them.

Specifically, the letter is a letter word comprising N. Wherein there are M illegible letters, characters used #instead.

Cuber QQ with the remnants of memories for each illegible letters are given a letter K different candidate.

In order to facilitate comparison which is closer to their own memories, Cuber QQ on paper a list of all possible words.

After reading these words, Cuber QQ believe in lexicographic order ranking, the first X word is the original word.

You know Cuber QQ before writing anything?

Input Format

The first line of the integer N, M, K and X (1≤N≤500000,1≤M≤N, 1≤K≤26,1≤X≤1018).

The second word line length is N, and lowercase letters #.

Next M rows, each row including letters K, denotes the i th letter illegible may be replaced by these letters.

To ensure that no more than X number of words can be constructed.

Output Format

Line a string that represents the answer.

Sample

input

5 2 3 4
c##nb
std
lws

output

cslnb

The original problem can be converted into \ (k-1 \) radix conversion issues
that Italy: # letter is uncertain, candidate letters of each row gives the following, according to the lexicographic composition Selection \ (X \) combinations is answer.
Lexicographic order, first order, a total of \ (k ^ m \) possible, since the number of candidates of each row are equal, so that a binary conversion problem.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 500000 + 5;

ll a[maxn];
char s[maxn];
char stemp[maxn];
char res[maxn];
ll len;

int main() 
{
    ll n,m,k,x;
    scanf("%lld%lld%lld%lld",&n,&m,&k,&x);
    x--;
    scanf("%s",s);
    int cnt=0;
    while(x)
    {
        a[cnt++]=x%k; x/=k;
    }
    cnt=0;
    for (int i = 0; i < m; i++)
    {
        scanf("%s", stemp);
        sort(stemp, stemp + k);
        res[cnt++]=stemp[a[m-i-1]];
    }
    cnt=0;
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '#')
        {
            printf("%c",res[cnt++]);
        }
        else printf("%c",s[i]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/chilkings/p/11963890.html