; 左向きは ^* 、右向きは *^ が特徴的な量なのでこの数を数える ; 文字列の中の文字列パターンの数を数える関数 ; (count-pattern-string "*^" "(^^*)(^^*)(*^^)(*^^)(^^*)(*^^)(*^^)#") ; => 4 ; (count-pattern-string "^*" "(^^*)(^^*)(*^^)(*^^)(^^*)(*^^)(*^^)#") ; => 3 (defun count-pattern-string (pattern str) (let ((count 0) ; str に表れる pattern の個数 (pos 0)) ; str に表れる pattern の位置 (loop ; str 中で pattern が表れた位置を更新しながら繰り返す (setq pos ; search sequence-1 sequence-2 &key from-end test test-not key start1 start2 end1 end2 ⇒ position ; sequence-2 の部分列と sequence-1 がマッチする場所を探す ; start2 キーワードは sequence-2 のスタート位置を設定するために使う (search pattern str :start2 pos)) (if pos (progn (incf count) (setq pos (1+ pos))) (return count))))) (defun main (&rest argv) (declare (ignorable argv)) (let* ((s (read-line)) (l (count-pattern-string "^*" s)) (r (count-pattern-string "*^" s))) (princ l) (write-char #\space) (princ r) (terpri))) (main)