CJam、25 − 25 = 0バイト
q~1,*_@{[\{1$^}/_](;)\}/;
これは、以下のGolfScript回答の単なるCJamポートです。MartinBüttner の回答を読んだ後、CJamの整数型と文字型の処理により1バイト節約できることに気付きました。(基本的に、CJamは、1&ASCII文字をGolfScriptコードのビットに強制するために使用する必要はありませんが、q入力を読み取るために先頭に追加する必要があります。)それは価値のあるIMOです。
いずれの場合でも、このプログラムは以下の元のGolfScriptプログラムとまったく同じように機能します。そのため、その説明と使用手順を参照してください。通常どおり、このオンラインインタープリターを使用してCJamバージョンをテストできます。
GolfScript、26 − 25 = 1バイト
~1,*.@{[1&\{1$^}/.](;)\}/;
このソリューションは、入力文字列を1回だけ反復するので、-25バイトのボーナスの資格があると思います。これは、k個の各事前反復の現在のビットを格納するk要素配列を内部的に維持することにより機能します。
入力は、stdinを介して次の形式で指定する必要があります。 "1111111" 3、つまり数字0と1文字の引用符付き文字列とそれに続く数字k。出力は、引用符なしのビット文字列として標準出力になります。
このコードをオンラインでテストします。(プログラムがタイムアウトした場合は、再実行してみてください。WebGolfScriptサーバーは、ランダムなタイムアウトで有名です。)
コメント付きのこのプログラムの拡張バージョンは次のとおりです。
~ # eval the input, leaving a string and the number k on the stack
1,* # turn the number k into an array of k zeros ("the state array")
. # make a copy of the array; it will be left on the stack, making up the
# first k bits of the output (which are always zeros)
@ # move the input string to the top of the stack, to be iterated over
{
[ # place a start-of-array marker on the stack, for later use
1& # zero out all but the lowest bit of this input byte
\ # move the state array to the top of the stack, to be iterated over
{ 1$^ } / # iterate over each element of the state array, XORing each
# element with the previous value on the stack, and leave
# the results on the stack
. # duplicate the last value on the stack (which is the output bit we want)
] # collect all values put on the stack since the last [ into an array
(; # remove the first element of the array (the input bit)
) # pop the last element (the duplicated output bit) off the array
\ # move the popped bit below the new state array on the stack
}
/ # iterate the preceding code block over the bytes in the input string
; # discard the state array, leaving just the output bits on the stack
基本的に、ほとんどの反復ソリューションのように、このコードは繰り返しを適用するものとして理解できます。
B I、J:= B I、(J -1) ⊕ B (I -1)、(J -1) 、
ここで、b 0、jはj番目の入力ビット(j≥1の場合)、b k、jはj番目の出力ビット、b i、0 = 0は仮定によります。差は(すなわち、第1すなわち、反復溶液に対し、実際には、「行ごと」再発計算され、B 1、jのすべてについてJ次に、B 2、J、等)、この解決策は、代わりに「列でそれを計算します「(より正確に、または、 "対角線による対角")列、第1の計算B I、 Iを 1ため≤ iが≤ K、次いでB I、I +1次に、B 、I、I 2、等
このアプローチの1つの(理論的な)利点は、原則として、このメソッドはO(k)ストレージのみを使用して任意の長い入力文字列を処理できることです。もちろん、GolfScriptインタープリターはプログラムを実行する前に自動的にすべての入力をメモリに読み込みますが、ほとんどの場合この利点は無効になります。