{"id":376,"date":"2018-08-03T18:18:14","date_gmt":"2018-08-03T18:18:14","guid":{"rendered":"https:\/\/www.haoyuan.info\/?p=376"},"modified":"2018-08-04T17:33:10","modified_gmt":"2018-08-04T17:33:10","slug":"hdu-6342-problem-k-expression-in-memories-%e6%9d%ad%e7%94%b5%e5%a4%9a%e6%a0%a1%e7%ac%ac%e5%9b%9b%e5%9c%ba","status":"publish","type":"post","link":"https:\/\/haoyuan.info\/?p=376","title":{"rendered":"HDU 6342 Problem K. Expression in Memories \u676d\u7535\u591a\u6821\u7b2c\u56db\u573a"},"content":{"rendered":"<h2>\u9898\u610f<\/h2>\n<p>\u89e3\u6790\u9898\u76ee\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5<br \/>\n\u82e5\u5408\u6cd5\u8f93\u51fa\u5408\u6cd5\u4e32<br \/>\n\u4e0d\u7b26\u5408\u8f93\u51faIMPOSSIBLE<br \/>\n?\u4ee3\u8868\u53ef\u4ee5\u4efb\u610f\u586b\u5199\u5b57\u7b26<br \/>\n\u8f93\u51fa\u4efb\u610f\u6ee1\u8db3\u9898\u610f\u7b54\u6848\u5373\u53ef<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u6839\u636e\u9898\u76ee\u8fdb\u884c\u5224\u65ad<br \/>\n\u4efb\u4f55\u64cd\u4f5c\u7b26\u90fd\u4e0d\u80fd\u5728\u5f00\u5934\u548c\u7ed3\u5c3e<br \/>\n\u4e0d\u80fd\u5b58\u5728\u524d\u7f00\u96f6<br \/>\n\u5206\u6790?\u53f7\u5e94\u8be5\u586b\u5199\u7684\u60c5\u51b5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5e94\u8be5\u662f\u5148\u586b\u5199\u6570\u5b57<br \/>\n\u4f46\u662f\u5b58\u5728\u586b\u5199\u6570\u5b57\u5bfc\u81f4\u5de6\u8fb9\u7684\u6570\u51fa\u73b0\u524d\u7f00\u96f6\u7684\u60c5\u51b5\uff0c\u5982<br \/>\n<code>0?1<\/code> -> <code>001<\/code> \u8fd9\u4e2a\u65f6\u5019\u53ea\u80fd\u586b\u5199\u4efb\u610f\u7684\u64cd\u4f5c\u7b26<br \/>\n\u6ce8\u610f\u5904\u7406\u7ec6\u8282\u5373\u53ef<\/p>\n<h2>AC\u4ee3\u7801<\/h2>\n<pre><code class=\"language-cpp  line-numbers\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;array&gt;\n#include &lt;cstring&gt;\n#include &lt;vector&gt;\n\n#define NUMBER 0\n#define OPERATOR 1\n#define UNKNOWN 2\nusing ll = long long;\nusing namespace std;\n\nint judge(char t) {\n    if (t &gt;= '0' &amp;&amp; t &lt;= '9') {\n        return NUMBER;\n    } else if (t == '+' || t == '*') {\n        return OPERATOR;\n    } else {\n        return UNKNOWN;\n    }\n}\n\nbool isprezero(string s, int pos) {\n    int cnt = 1;\n    while (pos &gt;= 0) {\n        int tp = judge(s[pos]);\n        if (tp == OPERATOR) {\n            if (s[pos + 1] == '0') {\n                return true;\n            } else {\n                return false;\n            }\n        }\n        --pos;\n    }\n    return s[0] == '0';\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    cout.tie(0);\n    int T;\n    cin &gt;&gt; T;\n    while (T--) {\n        string s;\n        string exp;\n        cin &gt;&gt; s;\n        int len = s.length();\n        int prev_type = judge(s[0]);\n        if (prev_type == NUMBER) {\n            if (s[0] == '0' &amp;&amp; judge(s[1]) == NUMBER) {\n                cout &lt;&lt; \"IMPOSSIBLE\" &lt;&lt; \"\\n\";\n                continue;\n            } else {\n                exp += s[0];\n            }\n        } else if (prev_type == OPERATOR) {\n            cout &lt;&lt; \"IMPOSSIBLE\" &lt;&lt; \"\\n\";\n            continue;\n        } else {\n            prev_type = NUMBER;\n            exp += '1';\n        }\n        bool valid = true;\n        for (int i = 1; i &lt; len; ++i) {\n            int tp = judge(s[i]);\n            if (tp == OPERATOR &amp;&amp; (prev_type == tp || i + 1 &gt;= len)) {\n                cout &lt;&lt; \"IMPOSSIBLE\" &lt;&lt; \"\\n\";\n                valid = false;\n                break;\n            } else if (tp == NUMBER &amp;&amp; prev_type == OPERATOR\n                       &amp;&amp; i + 1 &lt; len &amp;&amp; (judge(s[i + 1]) == NUMBER || judge(s[i + 1]) == UNKNOWN)) {\n                if (s[i] == '0' &amp;&amp; judge(s[i + 1]) == NUMBER) {\n                    cout &lt;&lt; \"IMPOSSIBLE\" &lt;&lt; \"\\n\";\n                    valid = false;\n                    break;\n                } else if (s[i] == '0' &amp;&amp; judge(s[i + 1]) == UNKNOWN) {\n                    s[i + 1] = '+';\n                }\n            } else if (tp == UNKNOWN) {\n                if (isprezero(s, i - 1)) {\n                    if (i + 1 &lt; len) {\n                        s[i] = '+';\n                        exp += '+';\n                        prev_type = OPERATOR;\n                    } else {\n                        cout &lt;&lt; \"IMPOSSIBLE\" &lt;&lt; \"\\n\";\n                        valid = false;\n                        break;\n                    }\n                    continue;\n                } else {\n                    prev_type = NUMBER;\n                    exp += '1';\n                    continue;\n                }\n            }\n            prev_type = tp;\n            exp += s[i];\n        }\n        if (valid) {\n            cout &lt;&lt; exp &lt;&lt; \"\\n\";\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u610f \u89e3\u6790\u9898\u76ee\u7684\u8868\u8fbe\u5f0f\u662f\u5426\u5408\u6cd5 \u82e5\u5408\u6cd5\u8f93\u51fa\u5408\u6cd5\u4e32 \u4e0d\u7b26\u5408\u8f93\u51faIMPOSSIBLE ?\u4ee3\u8868\u53ef\u4ee5\u4efb\u610f\u586b\u5199\u5b57\u7b26 \u8f93\u51fa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[39,51,22],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-c","category-51","category-22"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8UC2c-64","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":1,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions\/377"}],"wp:attachment":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}