가장 짧은 스 와이프 경로 찾기 1) 3) slide to `l` (line segments

소개

최근에 Swype 로 타이핑하는 데 익숙해졌습니다 .

시작 글자에서 끝 글자까지 직선을 그리거나 반복되는 글자를 건너 뛰어 특정 단어를 만들 수 있음을 알았습니다.

예를 들어 balloon다음 문자를 Swyping 하여 단어 를 입력 할 수 있습니다 .

b> a> l> o> n.

도전

우리가 정의 할 수있는 최단있는 Swype 경로 또는 SSP문자열을 입력 할 필요 구별 선분의 최소 숫자로. 선분은 둘 이상의 문자 사이의 연속적인 직선입니다. 방향이 바뀌면 새로운 선분이 시작되지만 단 하나의 직선 만 그려서 일부 단어를 스위핑 할 ​​수도 있습니다.

이 간단한 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것이다 SSP4다음 순서를 설명하는대로 :

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 할 때 방향을 변경할 필요가 없으므로 문자열 qwertySSP= 1입니다.

입력

a-zvia STDIN, 함수 인수 또는 명령 행을 포함하는 단일 단어 문자열 .

산출

n문자열을 나타내는 숫자 인 STDOUT, return 또는 가장 가까운 언어로 인쇄하십시오 SSP.

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


답변

CJam, 78 76 73 68 62 바이트

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

코드에는 인쇄 할 수없는 문자가 포함되어 있습니다.

6 바이트를 절약 한 경로를 계산하기 위해 RLE를 사용하는 @isaacg의 영리한 아이디어를 차용합니다.

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.

답변

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.

답변