fundo
A transformação Burrows – Wheeler (BWT) é uma permutação reversível dos caracteres de uma string que resulta em grandes execuções de caracteres semelhantes para certos tipos de strings, como texto sem formatação. É usado, por exemplo, no algoritmo de compactação bzip2 .
O BWT é definido da seguinte maneira:
Dada uma sequência de entrada, como codegolf
, calcule todas as rotações possíveis e classifique-as em ordem lexicográfica:
codegolf
degolfco
egolfcod
fcodegol
golfcode
lfcodego
odegolfc
olfcodeg
O BWT da string codegolf
é a string que consiste no último caractere de cada string nessa ordem, ou seja, a última coluna do bloco acima. Pois codegolf
, isso produz fodleocg
.
Por si só, essa transformação não é reversível, pois as strings codegolf
e golfcode
resultam na mesma string. No entanto, se soubermos que a sequência termina com um f
, existe apenas uma pré-imagem possível.
Tarefa
Implemente um programa ou função involutivo que leia uma única string de STDIN ou como argumento de linha de comando ou função e imprima ou retorne o BWT ou seu inverso da string de entrada.
Se a sequência de entrada não contiver espaços, seu envio deverá anexar um único espaço à entrada e calcular o BWT.
Se a sequência de entrada já contiver um espaço, deverá calcular a pré-imagem do BWT que possui um espaço à direita e retirar esse espaço.
Exemplos
INPUT: ProgrammingPuzzles&CodeGolf
OUTPUT: fs&e grodllnomzomaiCrGgPePzu
INPUT: fs&e grodllnomzomaiCrGgPePzu
OUTPUT: ProgrammingPuzzles&CodeGolf
INPUT: bt4{2UK<({ZyJ>LqQQDL6!d,@:~L"#Da\6%EYp%y_{ed2GNmF"1<PkB3tFbyk@u0#^UZ<52-@bw@n%m5xge2w0HeoM#4zaT:OrI1I<|f#jy`V9tGZA5su*b7X:Xn%L|9MX@\2W_NwQ^)2Yc*1b7W<^iY2i2Kr[mB;,c>^}Z]>kT6_c(4}hIJAR~x^HW?l1+^5\VW'\)`h{6:TZ)^#lJyH|J2Jzn=V6cyp&eXo4]el1W`AQpHCCYpc;5Tu@$[P?)_a?-RV82[):[@94{*#!;m8k"LXT~5EYyD<z=n`Gfn/;%}did\fw+/AzVuz]7^N%vm1lJ)PK*-]H~I5ixZ1*Cn]k%dxiQ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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OUTPUT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Regras adicionais
Você não pode usar nenhum operador interno que calcule o BWT (ou seu inverso) de uma sequência.
Você não pode usar nenhum operador interno que inverta funções.
Seu código pode imprimir uma nova linha à direita se você escolher STDOUT para saída, mesmo se ele não suportar novas linhas na entrada.
Seu código precisa funcionar para qualquer entrada de 500 ou menos caracteres ASCII imprimíveis (0x20 a 0x7E), incluindo no máximo um espaço.
Para qualquer uma das entradas possíveis descritas acima, seu código deve terminar em menos de dez minutos na minha máquina (Intel Core i7-3770, 16 GiB RAM). O último caso de teste deve ser o mais lento, portanto, certifique-se de cronometrar seu código com esse.
Para a maioria dos idiomas e abordagens, deve ser fácil cumprir o prazo. Essa regra visa unicamente impedir a implementação de uma transformação como uma força bruta inversa da outra.
Aplicam-se regras de código padrão de golfe. O menor envio em bytes vence.
inv
.
inv
trabalho em uma função definida pelo usuário. Não tenho certeza se inv
funcionaria para o BWT, mas é melhor prevenir do que remediar.