(defun split (string &key (delimiterp #'delimiterp)) (loop :for beg = (position-if-not delimiterp string) :then (position-if-not delimiterp string :start (1+ end)) :for end = (and beg (position-if delimiterp string :start beg)) :when beg :collect (subseq string beg end) :while end)) (defun delimiterp (c) (or (char= c #\Space) (char= c #\,))) (defun split-to-chars-by-space (str num &optional (result ())) (if (< num (length str)) (if (char= (char str num) #\Space) (split-to-chars-by-space str (+ 1 num) result) (split-to-chars-by-space str (+ 1 num) (cons (char str num) result))) result)) ; (split-to-fun-chars-by-space "1 0 1 1 0" 0 #'char-bool-to-p-str) ; -> ("¬P_5" "P_4" "P_3" "¬P_2" "P_1") (defun split-to-fun-chars-by-space (str num f &optional (result ())) (if (< num (length str)) (if (char= (char str num) #\Space) (split-to-fun-chars-by-space str (+ 1 num) f result) (split-to-fun-chars-by-space str (+ 1 num) f (cons (funcall f (+ 1 (length result)) (char str num)) result))) result)) ; (char-bool-to-p-str 1 #\0) -> ¬P_1 ; (char-bool-to-p-str 2 #\1) -> P_2 (defun char-bool-to-p-str (num c) (if (char= #\0 c) (format nil "¬P_~A" num) (format nil "P_~A" num))) (defun solve-line (n s) (if (char= (char s (- (length s) 1)) #\0) nil (split-to-fun-chars-by-space (subseq s 0 (- (length s) 1)) 0 #'char-bool-to-p-str))) (defun solve-lines (n limit &optional (input ())) (if (= 0 limit) input (solve-lines n (- limit 1) (cons (solve-line n (read-line)) input)))) (defun output-lines (lines &optional (sep "")) (if (= (length lines) 0) nil (if (car lines) (progn (format t "~A(~{~A~^∧~})" sep (reverse (car lines))) (output-lines (cdr lines) "∨")) (output-lines (cdr lines))))) (let* ((n (parse-integer (read-line))) (m (solve-lines n (expt 2 n)))) (output-lines (reverse m)))