• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            ONP - SPOJ 4. Transform the Expression

            Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation). Two-argument operators: +, -, *, /, ^ (priority from the lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z. Assume that there is only one RPN form (no expressions like a*b*c).

            Input

            t [the number of expressions <= 100]
            expression [length <= 400]
            [other expressions]
            

            Text grouped in [ ] does not appear in the input file.

            Output

            The expressions in RPN form, one per line.
            

            Example

            Input:
            3
            (a+(b*c))
            ((a+b)*(z+x))
            ((a+t)*((b+(a+c))^(c+d)))
            
            Output:
            abc*+
            ab+zx+*
            at+bac++cd+^*
             
             
            中綴轉(zhuǎn)后綴,用遞歸解決。
            lambda 很好用。
            LISP SBCL
             1(defconstant +max-len+ 420)
             2(defconstant +max-pri+ 123456789)
             3(defvar *str*)
             4(defvar *pri*)
             5
             6
             7(defun solve(le ri)
             8 (let ((min-pri +max-pri+)
             9       (cur-idx le)
            10       (min-idx +max-len+))
            11  (count -1 *pri* :key #'(lambda (x)
            12                          (when (> min-pri x)
            13                           (setf min-pri x)
            14                           (setf min-idx cur-idx))
            15                          (incf cur-idx))
            16                  :start le
            17                  :end   ri)
            18  (cond
            19   ((< min-pri +max-pri+)
            20    (solve le min-idx)
            21    (solve (1+ min-idx) ri)
            22    (write-char (elt *str* min-idx)))
            23   ((= min-pri +max-pri+)
            24    (write-string (remove #\( (remove #\) (subseq *str* le ri))))))))
            25
            26
            27(let ((num (read))
            28      (tot-pri 0))
            29 (dotimes (i num)
            30  (setf *str* (remove #\Space (read-line)))
            31  (setf *pri* (make-array +max-len+ :fill-pointer 0))
            32  (setf tot-pri 0)
            33  (count #\? *str* :key #'(lambda (x)
            34                           (cond
            35                            ((alpha-char-p x)
            36                             (vector-push +max-pri+ *pri*))
            37                            ((char= x #\+)
            38                             (vector-push (+ tot-pri 1*pri*))
            39                            ((char= x #\-)
            40                             (vector-push (+ tot-pri 2*pri*))
            41                            ((char= x #\*)
            42                             (vector-push (+ tot-pri 3*pri*))
            43                            ((char= x #\/)
            44                             (vector-push (+ tot-pri 4*pri*))
            45                            ((char= x #\^)
            46                             (vector-push (+ tot-pri 5*pri*))
            47                            ((char= x #\()
            48                             (incf tot-pri 6)
            49                             (vector-push +max-pri+ *pri*))
            50                            ((char= x #\))
            51                             (decf tot-pri 6)
            52                             (vector-push +max-pri+ *pri*))
            53                            (t 
            54                             (vector-push +max-pri+ *pri*)))
            55                           #\.))
            56  (solve 0 (length *str*))
            57  (fresh-line)))
            58
            59
             

            posted on 2012-02-19 10:34 coreBugZJ 閱讀(319) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACMAlgorithmLisp

            青春久久| 久久国产高清一区二区三区| 精品久久久久久无码不卡| 亚洲&#228;v永久无码精品天堂久久| 久久国产精品波多野结衣AV| 久久无码专区国产精品发布| 99久久国产热无码精品免费| 久久久久国产亚洲AV麻豆| 久久综合亚洲色HEZYO社区| 91久久精品91久久性色| 久久综合狠狠综合久久97色| 精品久久久久久无码专区不卡| 久久青青国产| 国产精品免费看久久久| 久久精品国产日本波多野结衣| 久久这里只精品国产99热| 国产激情久久久久久熟女老人| 9191精品国产免费久久| 久久狠狠高潮亚洲精品| 日韩欧美亚洲综合久久 | 久久久WWW成人| 久久ZYZ资源站无码中文动漫| 久久久久亚洲精品无码网址| 久久久久国产精品| 久久精品水蜜桃av综合天堂| 2020国产成人久久精品| 久久婷婷色综合一区二区| 国产亚洲色婷婷久久99精品91 | 性高朝久久久久久久久久| 成人a毛片久久免费播放| 国产精品久久网| 久久久久国产一级毛片高清版| 欧美一区二区三区久久综| 亚洲愉拍99热成人精品热久久| 伊色综合久久之综合久久| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | AV无码久久久久不卡蜜桃| 亚洲国产成人久久综合一区77 | 亚洲香蕉网久久综合影视| 久久精品国产久精国产一老狼| 伊人久久大香线蕉无码麻豆|