読者です 読者をやめる 読者になる 読者になる

Let's write β

趣味で書いたこととか、RustとLispが好き

JAG2011 ProblemA

ICPC Lisp

koukyoukoukokukikou

(defvar *qwarty-right* (coerce "yuiophjklnm" 'list))
(defvar *qwarty-left* (coerce "qwretasdfgzxcvb" 'list))

(defun count-hand-change (string)
  (let ((ch-list (coerce string 'list)))
    (labels
	((%count-hand-change (ch-list before-hand acc)
	   (if (null ch-list)
	       acc
	       (cond ((and (member (car ch-list) *qwarty-right*)
			   (equal :left before-hand))
		      (%count-hand-change (cdr ch-list)
					  :right (1+ acc)))
		     ((and (member (car ch-list) *qwarty-left*)
			   (equal :right before-hand))
		      (%count-hand-change (cdr ch-list)
					  :left (1+ acc)))
		     (t (%count-hand-change (cdr ch-list)
					    before-hand acc))))))
      (if (member (car ch-list) *qwarty-left*)
	  (%count-hand-change (cdr ch-list) :left 0)
	  (%count-hand-change (cdr ch-list) :right 0)))))

(defun main ()
  (loop for line = (read-line)
       until (string= "#" line)
       do
       (format t "~A~%" (count-hand-change line))))

再帰で。

僕が働いているAzit.incでは一緒に働けるエンジニアを募集しています!
採用情報 — 株式会社アジット|Azit Inc.