ค้นหาเส้นทาง Swype ที่สั้นที่สุด


12

บทนำ

เมื่อเร็ว ๆ นี้ฉันเริ่มชินกับการพิมพ์ด้วยSwypeแล้ว

ฉันสังเกตเห็นบางคำที่สามารถสร้างได้โดยการวาดเส้นตรงจากตัวอักษรเริ่มต้นของคุณไปยังตัวอักษรสุดท้ายของคุณหรือโดยการข้ามตัวอักษรที่ทำซ้ำ

ตัวอย่างเช่นฉันสามารถพิมพ์คำballoonโดย Swyping ผ่านตัวอักษรต่อไปนี้:

b> a> l> >on

ท้าทาย

ขอให้เรากำหนดเส้นทางที่สั้นที่สุด SwypeหรือSSPเป็นจำนวนขั้นต่ำของส่วนของเส้นที่แตกต่างที่จำเป็นในการพิมพ์สตริง ส่วนของเส้นตรงเป็นเส้นตรงต่อเนื่องระหว่างตัวอักษรสองตัวขึ้นไป การเปลี่ยนแปลงทิศทางใด ๆ จะเริ่มเซ็กเมนต์บรรทัดใหม่ - แม้ว่าคำบางคำอาจถูก Swyped โดยการวาดเพียงเส้นตรงเส้นเดียว

ใช้รูปแบบแป้นพิมพ์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จะมีSSPการ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)

สตริงqwertyมีSSP= 1 เนื่องจากไม่จำเป็นต้องเปลี่ยนทิศทางเมื่อทำการ Swyping คำนี้

อินพุต

สตริงคำเดียวที่มีใด ๆa-zผ่าน STDIN, อาร์กิวเมนต์ของฟังก์ชันหรือบรรทัดคำสั่ง

เอาท์พุต

พิมพ์ผ่าน STDOUT ผลตอบแทนหรือภาษาของคุณทางเลือกที่ใกล้เคียงที่สุดจำนวนตัวแทนของสตริงnSSP

ขึ้นบรรทัดใหม่หนึ่งตัวเลือกใน outut ช่องโหว่มาตรฐานไม่อนุญาต การส่งที่สั้นที่สุดในหน่วยไบต์ชนะ

หมายเหตุ

  • การเปลี่ยนทิศทางเริ่มต้นส่วนของบรรทัดใหม่
  • ตัวอักษรที่ทำซ้ำจะนับเพียงครั้งเดียว (เช่น: 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

หากการส่งต้องสนับสนุนตัวอักษรตัวพิมพ์ใหญ่เช่นกันฉันขอแนะนำให้รวมถึงบางส่วนในกรณีทดสอบ
Dennis

@ 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`,

โปรดทราบว่ารหัสมีอักขระที่ไม่สามารถพิมพ์ได้

ความคิดที่ชาญฉลาดของ @ isaacg ในการใช้ RLE เพื่อนับเส้นทางที่บันทึกไว้ 6 ไบต์

ลองใช้ออนไลน์ในล่าม CJam หากลิงก์ไม่ทำงานให้คัดลอกรหัสจากวางนี้

มันทำงานอย่างไร

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%! รอคอยที่จะเห็นคำอธิบายของคุณ
Dennis
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.