굴, 휠러 및 백


15

배경

그만큼 버로우즈 - 휠러 변환 (BWT) 캐릭터 라인의 문자 가역 순열가되도록 일반 텍스트로 문자열의 특정 유형의 유사 문자의 큰 실행 결과. 예를 들어 bzip2 압축 알고리즘에서 사용 됩니다.

BWT는 다음과 같이 정의됩니다.

와 같은 입력 문자열이 주어지면 codegolf가능한 모든 회전을 계산하고 사전 순서대로 정렬하십시오.

codegolf
degolfco
egolfcod
fcodegol
golfcode
lfcodego
odegolfc
olfcodeg

문자열의 BWT는 codegolf각 문자열의 마지막 문자, 즉 위의 블록의 마지막 열로 구성된 문자열입니다. 에 대한codegolf에 대해이 결과는 과 같습니다 fodleocg.

그 자체로,이 변환은 문자열 codegolfgolfcode 결과가 동일한 문자열 . 그러나 문자열이로 끝나는 것을 알고 있으면 f가능한 사전 이미지가 하나뿐입니다.

직무

STDIN에서 단일 문자열을 읽거나 명령 행 또는 함수 인수로 사용하여 BWT 또는 입력 문자열의 역수를 인쇄하거나 리턴하는 복잡한 프로그램 또는 함수를 구현하십시오.

입력 문자열에 공백이 없으면 제출물이 단일 공백을 입력에 추가하고 BWT를 계산해야합니다.

입력 문자열에 이미 공백이 있으면 후행 공백이있는 BWT의 사전 이미지를 계산하고 해당 공백을 제거해야합니다.

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?)_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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
OUTPUT: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

추가 규칙

  • 문자열의 BWT (또는 그 역수)를 계산하는 내장 연산자를 사용할 수 없습니다.

  • 기능을 반전시키는 내장 연산자를 사용할 수 없습니다.

  • 입력에서 후행 줄 바꿈을 지원하지 않더라도 출력에 STDOUT을 선택하면 코드에서 후행 줄 바꿈을 인쇄 할 수 있습니다.

  • 코드는 최대 1 개의 공백을 포함하여 500 개 이하의 인쇄 가능한 ASCII 문자 (0x20 ~ 0x7E)를 입력해야합니다.

  • 위에 설명 된 가능한 입력에 대해서는 코드가 내 컴퓨터 (Intel Core i7-3770, 16 GiB RAM)에서 10 분 이내에 완료되어야합니다. 마지막 테스트 케이스는 가장 느리므로 해당 코드로 코드를 작성하십시오.

    대부분의 언어와 접근 방식에서는 시간 제한을 쉽게 준수 할 수 있어야합니다. 이 규칙은 변형을 다른 것의 무차별 대입으로 구현하는 것을 막기위한 것입니다.

  • 표준 코드 골프 규칙이 적용됩니다. 바이트 단위로 가장 짧은 제출이 이깁니다.


"함수를 뒤집는 내장 연산자를 사용할 수 없습니다." 그런 일이 있으면 정말 놀랐습니다!
휴 앨런

4
@HughAllen J는 inv.
Dennis

사소하거나 내장 함수에서는 작동하지만 일반 사용자 정의 함수에서는 어떻게 작동합니까? 이 "inv"가 어딘가에 문서화되어 있습니까?
휴 알렌

@ HughAllen : 나는 J를 모른다.하지만 사용자 정의 함수 를 다루는 예제 가있다 inv. invBWT에서 작동 하는지 확실하지 않지만 죄송합니다.
Dennis

전체 bzip2에 대해서는 codegolf.stackexchange.com/questions/4771/… 을 참조하십시오 .
Keith Randall

답변:


9

Pyth, 29 바이트

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

데모. 테스트 하네스.

이 코드는 인코딩 및 디코딩 세그먼트로 간단하게 나뉘며, 코드는 3 진법으로 사용할 코드를 결정합니다.

?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 바이트

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

여기에서 테스트하십시오.

설명

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

펄 5, 179

나에게서 또 다른 좋지 않은 것. 이것에서 아마 약간의 이득이있을 것입니다, 그러나 그것은 목적으로 만들어진 골프 언어와 경쟁하지 않을 것입니다.

$_=<>;@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}

언 골프 :

# 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;
}

몇 가지 사소한 개선 사항 : 1. -n스위치 를 사용하는 경우 (보통 1 바이트로 계산) 필요하지 않습니다 $_=<>;. 2. for(1..$#y){...}될 수 있습니다 ... for 1..$#y. 3. $"로 초기화 " "되고 1 바이트 더 짧습니다.
Dennis

@ 데니스 좋은 전화. 나는 for한 시점 에 여러 문장을 가지고 있었기 때문에 접미사 양식이 승리하지는 않았지만 그것을 파싱 할 때 눈치 채지 못했습니다 :)
hobbs
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.