最短のSwypeパスを見つける


12

前書き

最近、私はSwypeでの入力に慣れてきました。

開始文字から終了文字まで直線を描くか、繰り返される文字をスキップすることで、特定の単語を生成できることに気付きました。

たとえばballoon、次の文字をスワイピングして単語を入力できます。

b> a> l> o> n。

チャレンジ

Shortest Swype Pathを定義しましょう、またはSSP、文字列の入力に必要な識別可能な線分セグメントの最小数として。線分は、2つ以上の文字の間の連続した直線です。方向を変更すると新しい線分セグメントが開始されますが、一部の単語は1本の直線のみを描くことでスイープされる場合があります。

次のシンプルなQWERTYキーボードレイアウトを使用します。

q w e r t y u i o p
a s d f g h j k l
  z x c v b n m

上記の例では、次のシーケンスで詳述されているように、単語balloonにSSPof があり4ます。

1) Start at `b` (line segments = 0)
2) slide to `a` (line segments = 1)
3) slide to `l` (line segments = 2)
4) slide to `o` (line segments = 3)
5) slide to `n` (line segments = 4)

この単語をSwypingするときに方向を変更する必要がないため、文字列qwertyはSSP= 1です。

入力

a-zvia STDIN、関数引数、またはコマンドラインを含む単一の単語文字列。

出力

STDOUT、return、または言語に最も近い代替手段を使用してn、文字列のを表す番号を出力しますSSP。

oututでオプションの1つの末尾の改行。標準の抜け穴は許可されません。バイト単位の最短提出が勝ちです。

ノート

  • 方向を変更すると、新しい線分が始まります。
  • 繰り返される文字は1回だけカウントbookkeeperされます(例:として処理する必要がありますbokeper)。
  • 通常、Swpyeは、隣接する文字を調べて最良の推測を入力することにより、欠落した文字を修正します。この課題では、自然言語の増強、予測テキストまたはエラー修正がないと仮定します。
  • 大文字のA-Z入力は、対応する小文字の入力と同様に扱われます。
  • 0-9入力内の数字を無視します。
  • 対角線経路が許可されている- 、カバーレターその直線であるo、k、n、例えば、としてカウント1セグメント。この規則は、任意の斜めの斜面に適用されます(例:文字c、h、iラインです)。

例

Input          Output
---------------------
a                0
aa               0
aaaaaa           0
aaaaaabc         2
in               1
int              2
java             3
qwerty           1
chicago          5
balloon          4
BALLOON          4
typewriter       5
bookkeeper       6
stackexchange    11
2hello7          3
2HELLO7          3

hはcからiまでの行にありますが、cとoの間に文字はありませんか?
— スパー

大文字もサポートする必要がある場合は、テストケースに一部を含めることをお勧めします。
— デニス

@Sparr正しい。
— CzarMatt

@Dennis Good call-テストケースを追加しました。
— CzarMatt

Swypeでタイピングを楽しんでいますが、ラインのプログラミングについては知りません
— ...-mbomb007

回答:


8

CJam、78 76 73 68 62バイト

relA,s-:_2ew{:-},{{i"x8LÑejPG ÀÏi"225b28b=A+Ab}/.-:ma}%e`,

コードに印刷できない文字が含まれていることに注意してください。

RLEを使用して6バイト節約されたパスをカウントするという@isaacgの巧妙なアイデアを取り入れました。

CJamインタプリタでオンラインで試してください。リンクが機能しない場合は、このpasteからコードをコピーします。

使い方

rel      e# Read a token from STDIN and cast it to lowercase.
A,s-     e# Remove all digits.
:_       e# Duplicate each element of the argument (string/array).
2ew      e# Push all overlapping slices of length 2.
{:-},    e# Filter out slices where both elements are equal.
{        e# For each slice:
  {      e#   For each character of the slice:
    i    e#     Push its code point.

    "x8LÑejPG ÀÏi"225b28b

         e#     Convert the string from base 225 to base 28, pushing the array
         e#     [15 7 16 17 18 27 26 8 9 0 3 11 4 6 24 1 22 5 21 10 25 23 12 2 13 14].
         e#     These are the positions on the QWERTY keyboard, starting from the
         e#     upper left corner and going right.

    =    e#     Select the element that corresponds to the code point.

         e#     Arrays wrap around in CJam. Since 'a' has code point 97, the array has
         e#     length 26 and 97 % 26 == 19, 'a' corresponds to the 20th element.

    A+Ab e#     Add 10 and convert to base 10. Example: 8 -> [1 8]
  }/     e#
  .-     e#     Vectorized subtraction. [a b] [c d] -> [a-c b-d]
  :ma    e#     atan2. Pushes the angle of the direction. [y x] -> arctan(y/x)
}%       e#
e`       e# Apply run-length encoding.
,        e# Count the runs.

非常に賢い。ご説明ありがとうございます。
— CzarMatt

3

Pyth、53 50 49バイト

lrPMfT-VJm.jF.D@jC"XÖ;¿ìÇ×;¤ð$  _"28CdT@GrzZtJ8

@Dennisによるキーボード圧縮形式。

この回答には、印刷できない文字が含まれています。正しいコードについては、以下のリンクをご覧ください。

デモンストレーション。テストハーネス。

説明:

lrPMfT-VJm.jF.D@jC"..."28CdT@GrzZtJ8
                              rzZ      Convert input to lowercase.
                            @G         Take the intersection with G.
                                       This removes the numbers.
         m                             Map over the characters
                 C"..."                Treat the string as a base 256 number.
                j      28              Convert this number to base 28, giving:
                                       [15, 7, 16, 17, 18, 27, 26,  ... ]
                                       which is the character positions 
                                       from top left, rotated by 97 places.
               @         Cd            Index this list by ord(character)
             .D            T           divmod by 10, giving the x-y position.
          .jF                          Convert this to a complex number.
        J                              Save the result to J.
      -VJ                        tJ    Vectorize - over J and J[1:]. This gives
                                       pairwise diffeences between J's elements.
    fT                                 Filter the differences on being truthy.
                                       This removes letter pairs with no movement.
  PM                                   Map the remaining complex numbers to their
                                       phases, which indicates their directions.
 r                                 8   Run-length encode the result.
l                                      Print out the number of separate RLE groups.

20%以上短縮!あなたの説明を楽しみにしています。
— デニス
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.