私はその数を見たことがありません!


31

非空白文字の文字列を通過するプログラムを書く(あなたは彼らが数字であることを仮定してもよい0のは9、彼らが処理される方法で、何もこれに依存しない)と、以下の規則に従ってスペースを追加します。

  1. 現在のトークンを空の文字列とし、以前に発行されたトークンを空のセットにします。
  2. 文字列の文字を反復処理します。各文字について、最初に文字を現在のトークンに追加します。次に、現在のトークンが以前に発行されたトークンのセットにまだない場合、現在のトークンをそのセットに追加し、新しい現在のトークンを空の文字列にします。
  3. 文字列の最後に達したときに現在のトークンが空の場合、スペース文字で区切られた、以前に発行されたトークンを発行順に出力します。それ以外の場合は、元の文字列をそのまま出力します。

入力

STDINへの入力は、数字のシーケンスである必要があります。

出力

プログラムは、手順3で指定された結果を印刷する必要があります。

サンプル

サンプル入力

2015
10101010
4815162342
101010101010
3455121372425
123456789101112131415
314159265358979323846264338327950288419716939937

サンプル出力

2 0 1 5
10101010
4 8 1 5 16 2 3 42
1 0 10 101 01 010
3 4 5 51 2 1 37 24 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3 1 4 15 9 2 6 5 35 8 97 93 23 84 62 64 33 83 27 95 0 28 841 971 69 39 937

これはコードゴルフであるため、標準のCGルールが適用されます。バイト単位の最短プログラムが優先されます。

(コメントの説明を要求してください。私はまだこれに新しいです。ありがとう!)


10
4815162342ブラザ、あなたがそこで何をしたかわかります。
致命的

16
提案されたOEISエントリ:このプロセスによって少なくとも2つのセグメントに分割される番号。
マーティンエンダー

3
@IsmaelMiguelステップ5(他のステップと同様)は、一度に1桁だけ進めることができます。取得する1 0 10 と、次の反復で1(既に使用されている)が検出され(検索済み)、1つ進んで10(既に使用されている)検索101が行われます。その後、スペースが追加され、新しい0が表示されます。これは既に使用されていますが、文字列の最後にあります。したがって、出力はになりますが1 0 10 101 0、これは無効(0繰り返される)であり、スクリプトは単に入力文字列を出力する必要があります。すでに使用されていた1010場合にのみ作成できました101
ヤヌスバズジャケ

3
@kasperd No If a unique number cannot be formed at the end of the string, then the input should be printed verbatim10101010は分割できないため、そのまま印刷されます。
edc65

1
ただし、ステップ5に入ると、スペースはの後に1なり、繰り返しになります。そのため、代わりにスペース5で右に移動し、ステップ4で再び右に移動し、ステップ5に再び入り、を作成します101
ピーターテイラー

回答:


9

Pyth、22バイト

 faW!}=+kTYY~kdz?tkzsY

先行スペースが重要です。


13

網膜68 61バイト

+`\b(\w+?)(?<!\b\1 .*)(\w+)$
$1 $2
(?=.* (.+)$(?<=\b\1 .+)) 
<empty>

<empty>空の行です。3行目の末尾のスペースに注意してください-s。フラグを使用して、単一のファイルから上記のコードを実行できます。

説明

+`\b(\w+?)(?<!\b\1 .*)(\w+)$
$1 $2

この最初のステップでは、ルール1〜6を実装します。これは、文字列の変更が停止するまで繰り返し適用される正規表現の置換です(これ+が目的です)。各ステップで、文字列に左から右に1つのスペースを追加します(チャレンジのルールに従います)。正規表現は、文字列の既に処理された部分に現れていない数字の最短文字列と一致します。残りの文字列のプレフィックスを単語境界で見て、\bスペースを渡さずに文字列の最後に到達できることを確認し(\w+)$ます。後者はまた、ステップごとに1つの置換のみを実行することを保証します。

(?=.* (.+)$(?<=\b\1 .+)) 
<empty>

これは、文字列の最後のセグメントが文字列内の他のセグメントと同じである場合、すべてのスペース(正規表現の最後にある)に一致し、それらを空の文字列で置き換えます。つまり、ルール7を実装して、最終セグメントが無効になった場合、最初のステップを元に戻します。


11

Pyth、 24 23バイト

VzI!}=+kNYaY~k"";?kzjdY

ここで試してみてください

VzI!}=+kNYaY~k"";?kzjdY    Implicit: z=input(), k='', Y=[], d=' '
Vz              ;          For N in z:
     =+kN                    Append N to k
  I!}    Y                   Is the above not in Y?
          aY k               Append k to Y
            ~k""             After append, reset k to ''
                 ?k        Is k truthy (i.e. not '')
                   z       Print original input
                    jdY    Otherwise print Y joined on spaces

バイトを保存してくれた@FryAmTheEggmanに感謝します:o)


@FryAmTheEggman A良い呼び出しは、私はかなりのkの元の値を維持しようとする際に巻き込まれた
ソック

8

Python 3、92バイト

i,n,*o=input(),""
for c in i:n+=c;o,n=[o+[n],o,"",n][n in o::2]
print([" ".join(o),i][n>""])

基本的に、@ Willemのソリューションの非常にゴルフバージョンです。


[" ".join(o),i][n>""]
FryAmTheEggman

@FryAmTheEggmanああ、かっこいい、私はそれを試してみましたがbool(n)、考えていませんでしたn>""
orlp

6

Pythonの3、100の 99バイト

o=[];n="";i=input()
for c in i:
 n+=c
 if not n in o:o.append(n);n=""
print(i if n else" ".join(o))

2
バイト数を修正しました。また、からスペースを削除する必要がありelse "ます。
mbomb007

1
いくつかの一般的なゴルフまた、元のスコアは100バイトだったと思います。
FryAmTheEggman

よろしくお願いします!「else」の後のスペースを削除できることを知りませんでした。別の日が生き、別の日が学んだ:)
ウィレム

5

Brachylog、91バイト

:_:_{h:0<|bhN,?hh:NrcH,?hB(l1,-1=A;BbA),?rhL:I(mH:Ar:[L]c:1&;:[H]:\"~w \"w,L:[H]c:_:Ar:1&)}

これにより、変更する必要がある構文について多くのことがあることに気付きました...

説明

:_:_              § Creates a list [Input,[],[]] 
{...}             § Define a new predicate between the brackets and call it with the previous list as input
h:0<              § If the head of the input is negative, stop
|                 § Else
bhN,              § Second element of Input is called N
?hh:NrcH,         § N concatenated with the first element of Input is H
?hB(l1,-1=A;BbA), § Remaining digits A are either -1 if there's only one digit left or all the digits but the head otherwise
?rhL:I            § List of used integers is called L
(
   mH:Ar:[L]c:1&  § If H is already in L, call the predicate with input [A,H,L]
   ;              § Else
   :[H]:\"~w \"w, § Print H followed by a space
   L:[H]c:_:Ar:1& § Call the predicate with input [A,[],M] where M is L with H appended to it
)

4

CJam、26バイト

LLq{+_a2$&{a+L}|}/:X+X!S**

ここでテストしてください。

説明

L        e# Push an empty array to keep track if the previous segments.
L        e# Push an empty array to build the current segment.
q{       e# For each character in the input...
  +      e#   Add it to the current segment.
  _a2$&  e#   Duplicate and check if it's already in the segment list.
  {      e#   If not...
    a+L  e#     Add it to the list and push a new empty array for the next segment.
  }|
}/
:X+      e# Store the trailing segment in X and add it's *characters* to the list.
         e# For valid splittings, this trailing segment will be empty, so that the
         e# list remains unchanged.
X!       e# Push X again and take the logical NOT.
S*       e# Get that many spaces, i.e. 1 for valid segments and 0 otherwise.
*        e# Join the list with this string between elements.

3

JavaScript(ES6)、109

私の出力形式は、クエストの出力サンプルとまったく同じではありません(先頭にスペースがあります)。出力形式が指定されていないため、欠陥として見られません(プログラムは番号の後に番号を印刷する必要があります...

EcmaScript 6準拠のブラウザーで以下のスニペットを実行してテストします。Firefoxで開発され、最新のChromeでテストおよび実行されています。

/* Test: redirect console.log */ console.log=x=>O.innerHTML+=x+'\n';

F=s=>{for(z=s,b=l=o=' ';s[+l];)~o.search(b+(n=s.slice(0,++l)+b))||(s=s.slice(l),o+=n,l=0);console.log(s?z:o)}

/* Test cases */
test = [
  '2015',
,'10101010'
,'4815162342'
,'101010101010'
,'3455121372425'
,'123456789101112131415'
,'11312123133'
,'314159265358979323846264338327950288419716939937']

test.forEach(t=>{console.log('\n'+t);F(t)})
<pre id=O></pre>


2

GNU sed, 83 77 73 71 bytes

(Score one extra because we require -r flag)

h
s/./&_/
:
/(\b[^ ]+).*\b\1_/{
s/_(.)/\1_/
t
g
}
s/_(.)/ \1_/
t
s/_//

The inner loop tests for a repeated sequence and appends characters as needed until a unique number appears after the separator _. The outer loop moves _ along.

Expanded, annotated version:

#!/bin/sed -rf

# Stash original in hold space
h

# Add separator
s/./&_/

:
# If current candidate is a duplicate, ...
/(\b[^ ]+).*\b\1_/{
#  ...then attempt to lengthen it ...
s/_(.)/\1_/
# ... and repeat if we succeeded, ...
t
# ... otherwise, restore original string
g
}
# Insert a space, and move our separator along
s/_(.)/ \1_/
t

# Remove the separator if we still have it
s/_//

You could combine both the t's into one.
User112638726

Also /((\b[^ ]+).*\b\2)_/{ can be rewritten as /(\b[^ ]+).*\b\1_/{, no reason for 2 capture groups.
User112638726

No problem :), you need to change the reference to \1 though!
User112638726

1

Ruby, 57 + 1 = 58 bytes

s=''
l={}
$_.chars{|c|l[s<<c]||=s=''}
l[s]||$_=l.keys*' '

Uses the command line flag -p (or pl if your input has a trailing newline). Exploits several traits of Ruby Hash dictionaries: you can safely mutate the string you used to define a key without it changing the key (which doesn't work for other mutable types), .keys returns the keys in the order they were inserted, and the []||= operator provides a terse way of branching on whether a given key is already there.


1

Haskell, 105 bytes

f does it.

e""s""=unwords s
e t s""=concat s++t
e t s(c:r)|t&c`elem`s=e(t&c)s r|0<1=e""(s&(t&c))r
a&b=a++[b]
f=e""[]

1

PHP - 148 bytes

Cool challenge, lots of fun!

$x=fgets(STDIN);$w=array();$k='';$l='';for($i=0;$i<strlen($x);$i++){$k.=$x[$i];if(!isset($w[$k])){$l.=$k.' ';$w[$k]=1;$k='';}}echo strlen($k)?$x:$l;
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.