{"id":962,"date":"2024-06-19T12:23:20","date_gmt":"2024-06-19T04:23:20","guid":{"rendered":"https:\/\/seanxd.com\/?p=962"},"modified":"2024-06-25T16:56:21","modified_gmt":"2024-06-25T08:56:21","slug":"zerojudge-o078","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-o078\/","title":{"rendered":"ZeroJudge O078: Word Vacancy"},"content":{"rendered":"\n\n\n<p>\u7d66\u5b9a\u4e00\u500b\u5927\u5c0f\u70ba K \u500b\u5b57\u6bcd\u7684\u96c6\u5408\u548c\u5b57\u4e32 S\uff0c\u6c42\u51fa\u5728\u4f7f\u7528\u8a72\u96c6\u5408\u6240\u7d44\u51fa\u9577\u5ea6\u70ba L \u5b57\u4e32\u4e2d\uff0c\u4e0d\u70ba\u5b57\u4e32 S \u5b50\u5b57\u4e32\u7684\u6700\u5c0f\u5b57\u5178\u5e8f\u5b57\u4e32\u70ba\u4f55\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u5b57\u6bcd\u96c6\u5408 {a, c, m}\uff0c\u5176\u80fd\u7d44\u51fa\u9577\u5ea6\u70ba 2 \u7684\u5b57\u4e32\u5b57\u5178\u5e8f\u7531\u5c0f\u5230\u5927\u6392\u5e8f\u70ba aa &lt; ac \uff1c am \uff1c ca &lt; cc &lt; cm &lt; ma &lt; mc &lt; mm\u3002\u5b57\u4e32 S \u70ba\u300caccaamcm\u300d\uff0c\u56e0\u6b64 ma \u70ba\u4e0d\u5728 S \u5167\u7684\u6700\u5c0f\u5b57\u5178\u5e8f\u5b57\u4e32\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u7b2c\u4e00\u884c\u70ba\u4e00\u500b\u9577\u5ea6\u70ba K (1 &lt;= K &lt;= 10) \u7684\u5c0f\u5beb\u5b57\u6bcd\u5b57\u4e32\u4ee3\u8868\u5b57\u6bcd\u96c6\u5408\uff0c\u4fdd\u8b49\u5b57\u5143\u4e0d\u91cd\u8907\u4e14\u7167\u5b57\u5143\u7531\u5c0f\u5230\u5927\u6392\u5e8f\u3002<br>\u7b2c\u4e8c\u884c\u70ba\u4e00\u500b\u6b63\u6574\u6578 L (1 &lt;= L &lt;= 8\uff0c1 &lt;= K<sup>L<\/sup> &lt;= 6\uff0a10<sup>5<\/sup>)\u3002<br>\u7b2c\u4e09\u884c\u70ba\u5c0f\u5beb\u82f1\u6587\u5b57\u4e32 S (L &lt;= |S| &lt;= 5\uff0a10<sup>5<\/sup>)\u3002<\/td><td>\u8f38\u51fa\u6eff\u8db3\u984c\u76ee\u8981\u6c42\u7684\u6700\u5c0f\u5b57\u5178\u5e8f\u5b57\u4e32\u3002<\/td><\/tr><tr><td>acm<br>2<br>accaamcm<\/td><td>ma<\/td><\/tr><tr><td>dp<br>3<br>dddppdpd<\/td><td>pdd<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u4f7f\u7528 DFS \u53bb\u5c07\u6240\u6709\u7d44\u5408\u8a08\u7b97\u51fa\u4f86\uff0c\u56e0\u70ba\u53ef\u4ee5\u6709\u91cd\u8907\u7684\u5b57\u5143\uff0c\u6240\u4ee5 DFS \u7684\u53c3\u6578\u53ea\u9700\u8981\u76ee\u524d\u76f8\u52a0\u7684\u5b57\u4e32\u3002\u5ba3\u544a\u4e00\u500b Map \u4f86\u7d00\u9304\u6709\u54ea\u4e9b\u7d44\u5408\uff0c\u9019\u908a\u53d6\u540d\u53eb\u505a MAP\uff0c\u7576 DFS \u4e2d\u7684\u5b57\u4e32\u9577\u5ea6 == L \u6642\uff0c\u5c07 MAP[\u5b57\u4e32]++ \u4e26\u4e14 return\u3002<\/p>\n\n\n\n<p>\u8dd1\u4e00\u500b For\u8ff4\u5708 \u5f9e 0 \u5230 S.length()-L\uff0c\u4e26\u4e14\u5ba3\u544a\u53e6\u5916\u4e00\u500b Map \u4f86\u7d00\u9304 S \u4e2d\u9577\u5ea6\u70ba L \u7684\u5b50\u5b57\u4e32\u6709\u54ea\u4e9b\uff0c\u9019\u908a\u53d6\u540d\u53eb appear\u3002\u5728\u88e1\u9762\u518d\u8dd1\u4e00\u500b For\u8ff4\u5708 \u5f9e\u76ee\u524d\u7684 i \u5230 i+L\uff0c\u8981\u5f9e\u76ee\u524d\u7684\u4f4d\u7f6e\u52a0\u5b57\u4e32\u52a0\u5230 L \u7684\u9577\u5ea6\u3002\u52a0\u5b8c\u4e4b\u5f8c\u5c07 appear[\u525b\u525b\u52a0\u5b8c\u7684\u5b57\u4e32]++\u3002<\/p>\n\n\n\n<p>\u4e4b\u5f8c\u8dd1\u4e00\u500b for (auto it:MAP)\uff0c\u8981\u5c07\u525b\u525b K \u4e2d\u7684\u6240\u6709\u7d44\u5408\u505a\u5224\u65b7\uff0c\u5982\u679c appear[it.first] == 0\uff0c\u4ee3\u8868 S \u4e2d\u6c92\u6709\u9019\u500b\u5b50\u5b57\u4e32\uff0c\u4f46\u662f\u525b\u525b\u7684 DFS \u6709\u8a08\u7b97\u51fa\u9019\u500b it.first\uff0c\u9019\u500b\u5c31\u662f\u7b54\u6848\uff0c\u8f38\u51fa it.first \u4e4b\u5f8c\u5c07\u8ff4\u5708 break\u3002\u8dd1 for (auto it:MAP) \u7684\u6642\u5019\u6703\u4f9d\u7167\u5b57\u4e32\u7684\u9806\u5e8f\u8dd1\uff0c\u6240\u4ee5\u5148\u8dd1\u5230\u7684\u5c31\u6703\u662f\u8f03\u5c0f\u7684\u5b57\u5178\u5e8f\u5b57\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=o078\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge O078: \u7f3a\u5b57\u554f\u984c<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nstring K, str;\nint L;\nmap&lt;string, int&gt;MAP;\n\nvoid DFS(string add) {\n    if (add.length() == L) {\n        MAP[add]++;\n        return;\n    }\n    for (int i = 0; i&lt;K.length(); i++) DFS(add+K[i]);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    cin &gt;&gt; K &gt;&gt; L &gt;&gt; str;\n    DFS(&quot;&quot;);\n    map&lt;string, int&gt;appear;\n    for (int i = 0; i&lt;=str.length()-L; i++) {\n        string tmp = &quot;&quot;;\n        for (int j = i; j&lt;i+L; j++) tmp += str[j];\n        appear[tmp]++;\n    }\n    for (auto it:MAP) {\n        if (appear[it.first] == 0) {\n            cout &lt;&lt; it.first &lt;&lt; &quot;\\n&quot;;\n            break;\n        }\n    }\n}\n\n\/\/ZeroJudge O078\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7d66\u5b9a\u4e00\u500b\u5927\u5c0f\u70ba K \u500b\u5b57\u6bcd\u7684\u96c6\u5408\u548c\u5b57\u4e32 S\uff0c\u6c42\u51fa\u5728\u4f7f\u7528\u8a72\u96c6\u5408\u6240\u7d44\u51fa\u9577\u5ea6\u70ba L \u5b57\u4e32\u4e2d\uff0c\u4e0d\u70ba\u5b57\u4e32 S \u5b50\u5b57\u4e32\u7684\u6700 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[25],"tags":[21,14,8,11,9],"class_list":["post-962","post","type-post","status-publish","format-standard","hentry","category-ioi-apcs","tag-dfs","tag-map","tag-8","tag-11","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=962"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/962\/revisions\/992"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}