Burrows, Wheeler ve Back


15

Arka fon

Burrows-Wheeler dönüşümü (MDK) bir dize bir karakter geri dönüşümlü permütasyon olacak şekilde düz metin olarak dizeleri belirli türleri için benzer karakterlerin büyük seri ile sonuçlanır. Örneğin, bzip2 sıkıştırma algoritmasında kullanılır .

BWT aşağıdaki gibi tanımlanır:

Gibi bir girdi dizesi verildiğinde codegolf, bunun tüm olası rotasyonlarını hesaplayın ve sözcük sırasına göre sıralayın:

codegolf
degolfco
egolfcod
fcodegol
golfcode
lfcodego
odegolfc
olfcodeg

Dizenin BWT değeri, codegolfbu sırayla her dizenin son karakterinden, yani yukarıdaki bloğun son sütunundan oluşan dizedir. Çünkü codegolf, bu sonuç verir fodleocg.

Tek başına bu dönüşüm tersine çevrilemez, çünkü dizeler codegolfve golfcodeaynı dizeyle sonuçlanır. Bununla birlikte, dizenin bir ile biteceğini fbilersek, sadece bir olası preimage vardır.

Görev

STDIN'den veya komut satırı veya işlev bağımsız değişkeni olarak tek bir dize okuyan ve BWT'yi veya giriş dizesinin tersini yazdıran veya döndüren ilgili bir program veya işlev uygulayın.

Giriş dizesi boşluk içermiyorsa, gönderiminiz girişe tek bir boşluk eklemeli ve BWT'yi hesaplamalıdır.

Giriş dizesi zaten bir boşluk içeriyorsa, sondaki boşluğa sahip BWT'nin ön resmini hesaplamalı ve bu alanı soymalıdır.

Örnekler

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:T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aaaaaaaab aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OUTPUT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Ek kurallar

  • Bir dizenin BWT'sini (veya tersini) hesaplayan yerleşik işleçleri kullanamazsınız.

  • İşlevleri tersine çeviren yerleşik işleçleri kullanamazsınız.

  • Girişte STDOUT'u seçerseniz, girişteki sondaki yeni satırları desteklemese bile kodunuz bir satırsonu yazdırabilir.

  • Kodunuz, en fazla bir boşluk dahil olmak üzere 500 veya daha az yazdırılabilir ASCII karakteri (0x20 - 0x7E) girişi için çalışmalıdır.

  • Yukarıda açıklanan olası girişlerden herhangi biri için, kodun makinemde on dakikadan kısa sürede bitmesi gerekir (Intel Core i7-3770, 16 GiB RAM). Son test durumu en yavaş olmalıdır, bu nedenle kodunuzu bu kodla zamanladığınızdan emin olun.

    Çoğu dil ve yaklaşım için zaman sınırına uymak kolay olmalıdır. Bu kural sadece bir dönüşümün diğerinin kaba kuvvet olarak tersine çevrilmesini önlemeyi amaçlamaktadır.

  • Standart kod golf kuralları geçerlidir. Bayt cinsinden en kısa gönderim kazanır.


"İşlevleri tersine çeviren yerleşik işleçleri kullanamazsınız." Böyle bir şey varsa çok şaşırırdım!
Hugh Allen

4
@HughAllen J var inv.
Dennis

Önemsiz veya yerleşik işlevler için işe yarayabilir, ancak genel kullanıcı tanımlı işlevler için nasıl çalışabilir? Bu "inv" bir yerde belgelendi mi?
Hugh Allen

@HughAllen: Gerçekten J biliyorum, ama yok burada bir örnektir ait invkullanıcı tanımlı bir işlevi üzerinde çalışıyor. invBWT için çalışıp çalışmadığından emin değilim , ama üzgünümden daha güvenli.
Dennis

Yanıtlar:


9

Pyth, 29 bayt

?tthu+VzSGzz}dzseMS.>L+z\ hlz

Gösteri. Kablo demetini test edin.

Bu, kodun bir üçlü ile hangisinin kullanılacağına karar vererek, doğrudan kodlama ve kod çözme segmentine bölünür.

?tthu+VzSGzz}dzseMS.>L+z\ hlz
                                 Implicit:
                                 z = input()
                                 d = ' '

?           }dz                  If there is a space in the input,
    u     zz                     Update G to the result of the following,
                                 with G starting as z and repeating len(z) times:
     +V                          The vectorized sum of
       zSG                       z and sorted(G)
   h                             Take the first such result, which will consist of
                                 the first character of z followed by the
                                 first cyclic permuation of the pre-BWT string,
                                 which must start with ' '.
 tt                              Remove the first two characters and return.
                 L               Otherwise, left-map (map with the variable as the 
                                 left parameter and a constant as the right)
               .>                cyclic right shift
                  +z\            of z + ' '
                      hlz        over range(len(z)+1)
              S                  Sort the shifted strings,
            eM                   take their last charactes,
           s                     combine into one string and return.

6

CJam, 41 36 35 bayt

q:XS&LX,{X\.+$}*0=1>XS+_,,\fm<$zW>?

Burada test edin.

açıklama

q:X   e# Read STDIN and store it in X.
S&    e# Take the set intersection with " ". We'll use this as a truthy/falsy value to
      e# select the correct output later.

# Compute the iBWT:
LX,   e# Push an empty array, compute the length of X.
{     e# Run the following block that many times:
  X\  e# Push X and pull the other array on top.
  .+  e# Add the characters of X to the corresponding line of the other array,
      e# i.e. prepend X as a new column.
  $   e# Sort the rows.
}*
0=    e# Since we just sorted the rows, the first permutation of the output will be
      e# one starting with a space, followed by the string we actually want. So just
      e# pick the first permutation.
1>    e# Remove the leading space.

# Compute the BWT:
XS+   e# Push X and append a space.
_,    e# Get that string's length N.
,\    e# Turn it into a range [0 .. N-1], swap it with the string.
fm<   e# Map each value in the range to the string shifted left by that many characters.
$     e# Sort the permutations.
zW>   e# Transpose the grid and discard all lines but the last.

?     e# Choose between the iBWT and the BWT based on whether the input had a space.

2

Perl 5, 179

Benden iyi olmayan bir tane daha. Muhtemelen bu konuda bazı kazanımlar var, ancak amaçlanan golf dilleriyle rekabet etmeyecek.

$_=<>;@y=/./g;if(/ /){@x=sort@y;for(1..$#y){@x=sort map$y[$_].$x[$_],0..$#x}
say$x[0]=~/^ (.*)/}else{push@y," ";say map/(.)$/,sort map{$i=$_;join"",
map$y[($_+$i)%@y],0..$#y}0..$#y}

Un-golfed:

# Read input
$_ = <>;
# Get all the chars of the input
my @chars = /./g;

if (/ /) {
  # If there's a space, run the IBWT:
  # Make the first column of the table
  my @working = sort @chars;

  # For each remaining character
  for (1 .. $#chars) {
    # Add the input as a new column to the left of @working,
    # then sort @working again
    @working = sort map {
      $chars[$_] . $working[$_]
    } 0 .. $#working;
  }
  # Print the first element of @working (the one beginning with space), sans space
  say $working[0] =~ /^ (.*)/;
} else {
  # BWT
  # Add a space to the end of the string
  push @chars, " ";
  # Get all the rotations of the string and sort them
  @rows = sort map {
    my $offset = $_;
    join "", map {
      $chars[($_ + $offset) % @chars]
    } 0 .. $#chars
  } 0 .. $#chars;

  # Print all the last characters
  say map /(.)$/, @rows;
}

Birkaç küçük iyileştirme: 1. -nAnahtarı kullanırsanız (genellikle 1 bayt olarak sayılır), ihtiyacınız yoktur $_=<>;. 2. for(1..$#y){...}olabilir ... for 1..$#y. 3. $"başlatılır " "ve bir bayt daha kısadır.
Dennis

@Dennis iyi çağrı. Ben forbir noktada birden fazla ifade vardı, bu yüzden postfix formu bir galibiyet değildi, ama ben bir tane aşağı pared zaman fark etmedim :)
hobbs
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.