{"id":406,"date":"2011-10-12T12:21:32","date_gmt":"2011-10-12T04:21:32","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=406"},"modified":"2012-08-13T12:28:50","modified_gmt":"2012-08-13T04:28:50","slug":"zoj-3539-compress-the-string","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/zoj-3539-compress-the-string\/","title":{"rendered":"ZOJ 3539. Compress the String"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u2026 \u5b9a\u4e49\u67d0\u4e2a\u5b57\u4e32\u7684\u538b\u7f29\u7b97\u6cd5\uff0c\u7528\u4e00\u7ec4\u5b57\u4e32\u538b\u7f29\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<br \/>\n\u65b9\u6cd5\u662f\u524d\u9762\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f15\u7528\u5176\u540e\u9762\u51fa\u73b0\u7684\u5b57\u7b26\u4e32\u7136\u540e\u5728\u5176\u539f\u5730\u6269\u5c55\u3002\u3002\u3002<\/p>\n<p>\u8981\u6c42 s[1] \u5c55\u5f00\u4e4b\u540e\u662f\u539f\u5b57\u7b26\u4e32\uff0c\u7ed9\u5b9a\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6 N, \u6bcf\u4e2a\u5206\u89e3\u4e32\u7684\u9650\u5236\u957f\u5ea6 Li\u3002\u3002<br \/>\n\u95ee\u662f\u5426\u5b58\u5728\u65e0\u635f\u538b\u7f29\u7684\u65b9\u6848\u3002\u3002<br \/>\n( .. . N <= 4, Li <= 4 .. .Sigma = 26 .. .)\n\n<!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>\u3002\u6bd4\u8d5b\u7684\u65f6\u5019\u662f\u6ca1\u6709\u65f6\u95f4\u591f\u6211\u8fb9\u60f3\u8fb9\u5199\u4e86\u3002\u3002\u8d5b\u540e\u641e\u6389\u5427\u3002\u3002<br \/>\n\u3002\u3002\u8fd9\u4e2a\u641c\u7d22\u9898\u8fd8\u662f\u5f88\u6709\u60f3\u6cd5\u7684\uff08\u867d\u7136\u672c\u4f53\u4e5f\u53ea\u662f\u4e2a\u5f31\u5316\u7248\u800c\u5df2\u3002\u3002\u3002\u4e48\u3002\u3002\uff09\u3002<br \/>\n\u4f46\u662f\u8fd8\u662f\u5f88\u4e0d\u597d\u5199\u5427\u3002\u3002\u82b1\u4e86 2 \u4e2a\u5c0f\u65f6\u3002\u3002\u4e2d\u95f4\u8fd8 NC \u628a\u6700\u540e\u7684\u76ee\u6807\u4e32\u7406\u89e3\u6210 s[1] + s[2] + \u2026 + s[4] \u2026<br \/>\n\uff08\u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u7528.. \u81ea\u52a8\u673a\u4f18\u5316\u3002\u3002\u3002\uff09<\/p>\n<p>\u597d\u5427\u3002\u3002\u3002\u5bf9\u4e8e\u8fd9\u9898\u7684\u8bdd\u3002\u3002\u76f8\u4fe1\u6bcf\u4e2a\u4eba\u90fd\u4f1a\u6709\u4e0d\u540c\u7684\u5199\u6cd5\u3002\u3002\u5199\u4e0b\u6211\u7684\u5427\u3002\u3002<\/p>\n<p>\u9996\u5148\u3002\u6700\u540e\u7684\u5f85\u538b\u7f29\u4e32\u7684\u4fe1\u606f\u53ea\u76f4\u63a5\u548c s[1] \u5173\u8054\u3002\u3002\u4f46\u662f\u8981\u786e\u5b9a s[1] \u4e32\u7684\u8bdd\uff0c\u53c8\u5fc5\u987b\u8981\u6709\u540e\u9762\u6240\u6709\u4e32\u7684\u4fe1\u606f\u3002\u3002<br \/>\n\u8fd9\u6837\u641c\u7d22\u7684\u987a\u5e8f\u7684\u8bdd\u3002\u3002\u5fc5\u987b\u8981\u4ece\u540e\u9762\u7684\u4e32\u5f00\u59cb\u3002\u3002\u4f46\u662f\u8fd9\u6837\u53c8\u6ca1\u6709\u76f4\u63a5\u7684\u538b\u7f29\u4e32\u7684\u4fe1\u606f\u53ef\u4f9b\u526a\u679d\u6240\u4ee5\u5f88\u662f\u9ebb\u70e6\u3002\u3002<\/p>\n<p>\uff08\u5927\u6982\u6211\u8ba4\u4e3a\u7684\u8fd9\u9898\u4e3b\u8981\u7684\u96be\u70b9\u5c31\u5728\u4e0a\u9762\u8fd9\u91cc\u4e86\u3002\u3002\u800c\u663e\u7136\u4e0a\u9762\u90a3\u53e5\u8bdd\u53c8\u662f\u4e0d\u5bf9\u7684\u3002\u3002\u9898\u76ee\u91cc\u53ea\u6709 4 \u4e2a\u957f\u5ea6\u7684\u9650\u5236\u4fe1\u606f\u548c\u5f85\u538b\u7f29\u4e32\u7684\u4fe1\u606f\u3002\u3002<br \/>\n\u3002\u3002\u90a3\u4e48\u53ef\u4ee5\u5f53\u505a s[3], s[2], s[1] \u7684\u4e32\u4e00\u5b9a\u4f1a\u975e\u5e38\u7a00\u5c11\u3002\u3002\u4f46\u662f\u8fd9\u4e00\u6b65\u8981\u600e\u4e48\u505a\u5462\u3002\u3002\uff1f\u3002\u3002\u53ef\u4ee5\u5bf9\u5f85\u538b\u7f29\u4e32\u5206\u6790\u51fa\u4e00\u4e2a\u4fe1\u606f\u542b\u91cf\u7684\u6570\u5b57\u51fa\u6765\u4e48\uff1f\u3002\u3002\u3002<br \/>\n\u3002\u3002\u6267\u884c\u4f55\u79cd\u5b57\u7b26\u4e32\u7b97\u6cd5\uff1f\u6700\u5927\u91cd\u590d\u5b50\u4e32\u6269\u5c55\uff1f\u3002\u3002\u55ef\u55ef\u3002\u3002\u6709\u5f85\u6211\u8fdb\u4e00\u6b65\u6316\u6398\u3002\u3002\u3002\uff09<\/p>\n<p>\u6211\u7684\u60f3\u6cd5\u5c31\u662f\u8981\u5728\u5c3d\u91cf\u5c11\u7528\u526a\u679d\u7684\u60c5\u51b5\u3002\u3002\u800c\u901a\u8fc7\u5bf9\u641c\u7d22\u7ed3\u6784\u7684\u4f18\u5316\u6765\u8fbe\u5230\u76ee\u7684\u3002\u3002\u3002<br \/>\n\u3002\u6743\u8861\u4e4b\u540e\u3002\u3002\u6211\u51b3\u5b9a\u91c7\u53d6\u5916\u5c42\u7b2c\u4e00\u5c42 dfs() \u4ec5\u786e\u5b9a\u957f\u5ea6\u548c\u6269\u5c55\u4f4d\u7f6e\u3002\u3002\u5bf9\u6bcf\u4e00\u7ec4\u5408\u6cd5\u7684\u6a21\u7cca\u89e3\u3002\u3002\u3002<br \/>\n\u3002\u7b2c\u4e8c\u5c42\u518d check() \u5224\u5b9a\u5176\u662f\u5426\u6709\u771f\u7684\u5408\u6cd5\u7684\u65b9\u6cd5\u3002\u3002<\/p>\n<p>\u7136\u540e\u526a\u679d\u53ea\u4f7f\u7528\u4e24\u6761\u3002\u3002<\/p>\n<ol>\n<li>\u53ef\u884c\u6027\u526a\u679d\u3002\u3002\uff08\u5bf9\u5916\u5c42 dfs()\u3002\u3002\u8bb0\u5f55\u5f53\u524d\u6700\u957f\u7684\u5206\u89e3\u4e32\u957f\u5ea6\u3002\u3002\u5982\u679c\u4e0d\u52a0\u8fd9\u5c42\u526a\u679d\u3002\u3002\u3002\u5728\u641c\u7d22\u7684\u7b2c\u4e00\u9636\u6bb5\u5c31 TLE \u4e86\u3002\u3002\uff09<\/li>\n<li>\u6539\u53d8\u641c\u7d22\u987a\u5e8f\u3002\u3002(\u4ee5\u5bf9\u6709\u89e3\u7684\u60c5\u51b5\u4e0b\u5c3d\u53ef\u80fd\u53d1\u73b0\u6b63\u89e3\u3002\u3002\u8fd9\u526a\u679d\u4e00\u5411\u5f88\u80fd\u6253\u4e0d\u662f\u4e48\uff1f\u3002 \u3002\u3002)\u3002\u3002<\/li>\n<p>\uff08\u5c45\u7136\u8fd9\u5c31\u8fc7\u4e86\u3002\u3002\u53ef\u89c1\u6570\u636e\u8fd8\u662f\u5f31\u3002\u3002\uff09<\/p>\n<p>3770 ms\u3002\u3002\u4ee3\u7801\u957f\u5ea6 100 \u884c\u7684\u6837\u5b50\u3002\u3002\u3002<br \/>\n\uff08\u3002\u3002\u5b8c\u6574\u4ee3\u7801\u8bf7\u79fb\u6b65<a href=\"http:\/\/acm.hust.edu.cn:8080\/judge\/problem\/viewSource.action?id=246770\">\u8fd9\u91cc<\/a>\u3002\u3002\uff09\n<\/ol>\n<pre class=\"brush: cpp; collapse: false; first-line: 1; light: false; title: ZOJ 3539. Compress the String.cpp; toolbar: true; notranslate\" title=\"ZOJ 3539. Compress the String.cpp\">\r\n.. .\r\nconst int N = 4, M = 500, L = 4;\r\n \r\nchar S&#x5B;M]; int len;\r\nint s&#x5B;N]&#x5B;L]; int _l&#x5B;N], l&#x5B;N], ll&#x5B;N];\r\nint n;\r\n \r\nconst int Normal = -1;\r\nbool C&#x5B;N]; int st&#x5B;N];\r\nbool check(){\r\n \r\n    RST(C); C&#x5B;n-1] = true, st&#x5B;n-1] = 0;\r\n \r\n    DWN(k, n, 1) if (C&#x5B;k]){\r\n        int p = st&#x5B;k];\r\n        REP(i, l&#x5B;k]){\r\n \r\n#define j s&#x5B;k]&#x5B;i]\r\n \r\n            if (j == Normal){\r\n                ++p;\r\n            }\r\n            else {\r\n                if (C&#x5B;j]){\r\n                    REP(ii, ll&#x5B;j]){\r\n                        if (S&#x5B;p+ii] != S&#x5B;st&#x5B;j]+ii]) return false;\r\n                    }\r\n                }\r\n                else {\r\n                    C&#x5B;j] = true, st&#x5B;j] = p;\r\n                }\r\n                 p += ll&#x5B;j];\r\n            }\r\n        }\r\n    }\r\n \r\n    return true;\r\n}\r\n \r\ninline bool dfs1(int, int);\r\ninline bool dfs2(int, int, int);\r\nbool dfs2(int i, int k, int max_ll){\r\n    if (i == l&#x5B;k]){\r\n        ll&#x5B;k] = 0; REP(i, l&#x5B;k]) ll&#x5B;k] += s&#x5B;k]&#x5B;i] == Normal ? 1 : ll&#x5B;s&#x5B;k]&#x5B;i]];\r\n        return dfs1(k + 1, max(max_ll, ll&#x5B;k]));\r\n    }\r\n    else {\r\n        DWN_N(s&#x5B;k]&#x5B;i], k, -1){\r\n            if (dfs2(i+1, k, max_ll)) return true;\r\n        }\r\n    }\r\n    return false;\r\n}\r\n \r\nbool dfs1(int k = 0, int max_ll = 1){\r\n \r\n    int t = max_ll; FOR(i, k, n) t *= _l&#x5B;i];\r\n    if (t &lt; len) return false;\r\n \r\n    if (k == n){\r\n        return ll&#x5B;k-1] == len &amp;&amp; check();\r\n    }\r\n    else {\r\n        DWN_1_N(l&#x5B;k], _l&#x5B;k], 0){\r\n            if (dfs2(0, k, max_ll)) return true;\r\n        }\r\n    }\r\n    return false;\r\n}\r\n \r\nint main(){\r\n \r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n \r\n    while (scanf(&quot;%d&quot;, &amp;n) != EOF){\r\n        DWN(i, n, 0) RD(_l&#x5B;i]); RS(S); len = strlen(S); reverse(S, S + len);\r\n        puts(dfs1() ? &quot;Yes&quot; : &quot;No&quot;);\r\n    }\r\n}\r\n<\/pre>\n<h3>Further discussion: <\/h3>\n<p>\uff08\u4e0d\u6b23\u8d4f\u4e00\u4e0b <a href=\"community.topcoder.com\/longcontest\/stats\/?module=ViewOverview&#038;rd=14565\">ACrush \u5728 Marathon \u7248\u91cc\u9762\u53d1\u7684\u4ee3\u7801<\/a> \u4e48\uff1f\u3002\u3002\uff09\u3002<\/p>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/acm.zju.edu.cn\/onlinejudge\/showProblem.do?problemId=4494\">http:\/\/acm.zju.edu.cn\/onlinejudge\/showProblem.do?problemId=4494<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u2026 \u5b9a\u4e49\u67d0\u4e2a\u5b57\u4e32\u7684\u538b\u7f29\u7b97\u6cd5\uff0c\u7528\u4e00\u7ec4\u5b57\u4e32\u538b\u7f29\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002 \u65b9\u6cd5\u662f\u524d\u9762\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f15\u7528\u5176\u540e\u9762\u51fa\u73b0\u7684\u5b57\u7b26\u4e32\u7136\u540e\u5728\u5176\u539f\u5730\u6269\u5c55\u3002\u3002\u3002 \u8981\u6c42 s[1] \u5c55\u5f00\u4e4b\u540e\u662f\u539f\u5b57\u7b26\u4e32\uff0c\u7ed9\u5b9a\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6 N, \u6bcf\u4e2a\u5206\u89e3\u4e32\u7684\u9650\u5236\u957f\u5ea6 Li\u3002\u3002 \u95ee\u662f\u5426\u5b58\u5728\u65e0\u635f\u538b\u7f29\u7684\u65b9\u6848\u3002\u3002 ( .. . N<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[55],"tags":[],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-zoj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-6y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}