• <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+^*
             
             
            中綴轉后綴,用遞歸解決。
            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 閱讀(328) 評論(0)  編輯 收藏 引用 所屬分類: ACM 、Algorithm 、Lisp

            国产精品成人99久久久久91gav| 久久精品亚洲福利| 中文精品久久久久人妻| 久久精品国产亚洲Aⅴ香蕉 | 99精品国产免费久久久久久下载| 999久久久国产精品| 久久精品视频网| 久久精品国产福利国产秒| 狠狠狠色丁香婷婷综合久久五月 | 久久精品国产久精国产思思| 久久久久亚洲精品无码蜜桃| 色妞色综合久久夜夜| 精品久久久无码人妻中文字幕豆芽| 亚洲AV无码久久精品蜜桃| 97精品久久天干天天天按摩| 国产精品久久久久久福利漫画| 99久久免费国产特黄| 国产成人精品久久一区二区三区av | 999久久久国产精品| 久久天天日天天操综合伊人av| 亚洲欧美日韩精品久久亚洲区| 亚洲午夜久久久| 国产成人久久AV免费| 国产精自产拍久久久久久蜜| 色偷偷88欧美精品久久久| 久久亚洲sm情趣捆绑调教 | 人人狠狠综合久久亚洲婷婷 | 日韩一区二区三区视频久久| 无码人妻精品一区二区三区久久久 | 久久伊人精品青青草原高清| 欧美午夜精品久久久久久浪潮| 7777精品久久久大香线蕉| 91精品国产综合久久香蕉 | 国产A三级久久精品| 91亚洲国产成人久久精品网址| 一本色道久久88综合日韩精品 | 国内精品人妻无码久久久影院| 久久久受www免费人成| 久久久精品人妻一区二区三区四 | 久久一区二区免费播放| 国产精品久久免费|