{"id":140,"date":"2023-04-20T20:20:20","date_gmt":"2023-04-20T12:20:20","guid":{"rendered":"https:\/\/solost.cn\/?p=140"},"modified":"2023-04-20T20:20:20","modified_gmt":"2023-04-20T12:20:20","slug":"248","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=140","title":{"rendered":"2022\u5e74\u4e2d\u56fd\u5927\u5b66\u751f\u7a0b\u5e8f\u8bbe\u8ba1\u7ade\u8d5b\u5973\u751f\u4e13\u573a &#8211; I. \u5ba0\u7269\u5bf9\u6218"},"content":{"rendered":"<h2>\u9898\u76ee<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image1-8.png\" alt=\"image(1).png\" title=\"image(1).png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/codeforces.com\/gym\/104081\/problem\/I\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5e8f\u5217a,b\u6bcf\u4e2a\u5e8f\u5217\u4e2d\u90fd\u6709\u4e00\u4e9b\u5b57\u7b26\u4e32\uff0c\u518d\u7ed9\u4e00\u4e2a\u76ee\u6807\u4e32s\uff0c\u95ee\u4f60\u8fd9\u4e2as\u662f\u5426\u80fd\u7531ababab\u6216\u8005babab\u7c7b\u4f3c\u8fd9\u79cd\u5f62\u5f0f\u7ec4\u6210\uff0ca\u8868\u793aa\u5e8f\u5217\u91cc\u9762\u7684\u5b57\u7b26\u4e32\uff0cb\u8868\u793ab\u91cc\u9762\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u8fd9\u4e2a\u9898\u76ee\u521a\u5f00\u59cb\u60f3\u76f4\u63a5\u51d1\u5c31\u884c\uff0c\u4f46\u662f\u5199\u7684\u8fc7\u7a0b\u4e2d\u60f3\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679cs\u4e2d\u6709\u4e00\u6bb5\u4ecea\u548cb\u90fd\u53ef\u4ee5\u51d1\u51fa\u6765\uff0c\u4f46\u662f\u51d1\u51fa\u6765\u7684\u957f\u5ea6\u4e0d\u4e00\u6837\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u5bfc\u81f4\u4e0d\u4e00\u6837\u7684\u7ed3\u679c\uff0c<code>s = abcd~~~~~~<\/code>\uff0c <code>a = abcd; b = abc<\/code>\u8fd9\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4ecea\u4e2d\u9009\u7b2c\u4e00\u4e2a\u7b26\u5408\u7684\u60c5\u51b5\u90a3\u4e48\u53ef\u80fd\u5bfc\u81f4\u540e\u9762\u6ca1\u529e\u6cd5\u5339\u914d\u3002\u540e\u6765\u60f3\u7684\u662f\u5bbd\u641c\uff0c\u7136\u540e\u8d85\u65f6\uff0c\u4f18\u5316\u4e86\u4e00\u70b9\u8d85\u7a7a\u95f4\u3002\u6240\u4ee5\u8fd9\u9053\u9898\u4e0d\u80fd\u7528\u66b4\u529b\u6765\u5199\u3002\u770b\u4e86\u773c\u9898\u89e3\u3002\u6b63\u89e3\u662fdp+trie\u6811\u3002<\/p>\n<p>\u8f6c\u79fb\u65b9\u7a0b<code>f[i][op] = min(f[i][op], f[j - 1][!op]<\/code>\u4ee3\u8868\u542b\u4e49\u662f\u76ee\u6807\u4e32s\u7684\u7b2ci\u4e2a\u5b57\u7b26\uff0c\u662f\u5426\u53ef\u7531op\u7684\u76f8\u53cd\u9762\u8f6c\u79fb\u800c\u6765\uff0c<code>j\u662f\u6307\u4ecei\u52301\u8fd9\u4e00\u6bb5\uff0c\u662f\u5426\u5728a\u6216\u8005b\u53d6\u51b3\u4e8e\u5f53\u524dop\u5b58\u5728\u4e00\u4e2a\u4e32\u80fd\u5339\u914dj-i\u8fd9\u4e00\u6bb5\u76ee\u6807\u4e32<\/code>\u3002<\/p>\n<p>\u96be\u70b9\u662f\u5982\u4f55\u5feb\u901f\u7684\u6c42\u51fa\u662f\u5426\u5b58\u5728\u8fd9\u6837\u7684\u4e00\u4e2a\u5b57\u4e32\u80fd\u591f\u4f7f\u5f97\u8f6c\u79fb\u5230\u5f53\u524d\u72b6\u6001<code>f[i][op]<\/code>,\u7136\u540e\u5c31\u53ef\u4ee5\u7528\u5230trie\u6811\u4e86\u3002\u4e3a\u4e86\u5728\u68c0\u67e5\u7684\u65f6\u5019\u597d\u68c0\u67e5\uff0c\u6211\u4eec\u53ef\u4ee5\u628aa\u7c7b\u548cb\u7c7b\u5efa\u4e00\u4e2a\u4e8c\u7ef4trie\u6811\uff0c\u7136\u540e\u6765\u8ba1\u7b97\uff0c\u5b58\u7684\u65f6\u5019\u53cd\u7740\u5b58\u3002\u68c0\u67e5\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u4ecei,i-1,i-2\uff0c\u65b9\u4fbf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u7684\u4e66\u5199\u3002<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iomanip&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n#include &lt;vector&gt;\n\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 5e5 + 10;\n\nint n, m;\nint f[N][2];\nint tr[26][N][2], cnt[N][2], idx[2];\n\nvoid insert(string &amp;s, int op)\n{\n    int p = 0;\n    for (int i = 0; i &lt; s.size(); i++) {\n        int u = s[i] - &#039;a&#039;;\n        if (!tr[u][p][op])\n            tr[u][p][op] = ++idx[op];\n        p = tr[u][p][op];\n    }\n    cnt[p][op]++;\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n;\n    string x;\n    for (int i = 1; i &lt;= n; i++) {\n        cin &gt;&gt; x;\n        reverse(x.begin(), x.end());\n        insert(x, 0);\n    }\n    cin &gt;&gt; m;\n    for (int i = 1; i &lt;= m; i++) {\n        cin &gt;&gt; x;\n        reverse(x.begin(), x.end());\n        insert(x, 1);\n    }\n    string s;\n    cin &gt;&gt; s;\n    int l = s.size();\n    s = &quot; &quot; + s;\n    memset(f, 0x3f, sizeof f);\n    f[0][0] = 0;\n    f[0][1] = 0;\n    for (int i = 1; i &lt;= l; i++) {\n        for (int op = 0; op &lt; 2; op++) {\n            int p = 0;\n            for (int j = i; j; j--) {\n                int u = s[j] - &#039;a&#039;;\n                if (!tr[u][p][!op])\n                    break;\n                p = tr[u][p][!op];\n                if (cnt[p][!op])\n                    f[i][op] = min(f[i][op], f[j - 1][!op] + 1);\n            }\n        }\n    }\n    int ans = min(f[l][0], f[l][1]);\n    if (ans == INF)\n        cout &lt;&lt; -1 &lt;&lt; endl;\n    else\n        cout &lt;&lt; ans &lt;&lt; endl;\n}\nint main()\n{\n    \/\/ IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u7ed9\u4f60\u4e24\u4e2a\u5e8f\u5217a,b\u6bcf\u4e2a\u5e8f\u5217\u4e2d\u90fd\u6709\u4e00\u4e9b\u5b57\u7b26\u4e32\uff0c\u518d\u7ed9\u4e00\u4e2a\u76ee\u6807\u4e32s\uff0c\u95ee\u4f60\u8fd9\u4e2as\u662f\u5426\u80fd\u7531ababab\u6216\u8005babab\u7c7b\u4f3c\u8fd9\u79cd\u5f62\u5f0f\u7ec4\u6210\uff0ca\u8868\u793aa\u5e8f\u5217\u91cc\u9762\u7684\u5b57\u7b26\u4e32\uff0cb\u8868\u793ab\u91cc\u9762\u7684\u5b57\u7b26\u4e32\u3002 \u9898\u89e3 \u8fd9\u4e2a\u9898\u76ee\u521a\u5f00\u59cb\u60f3\u76f4\u63a5\u51d1\u5c31\u884c\uff0c\u4f46\u662f\u5199\u7684\u8fc7\u7a0b\u4e2d\u60f3\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679cs\u4e2d\u6709\u4e00\u6bb5\u4ecea\u548cb\u90fd\u53ef\u4ee5\u51d1\u51fa\u6765\uff0c\u4f46\u662f\u51d1\u51fa\u6765\u7684\u957f\u5ea6\u4e0d\u4e00\u6837\uff0c\u90a3\u4e48\u5c31\u53ef\u80fd\u5bfc\u81f4\u4e0d\u4e00\u6837\u7684\u7ed3\u679c\uff0cs = abcd~~~~~~\uff0c a = abcd; b = abc\u8fd9\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4ecea\u4e2d\u9009\u7b2c\u4e00\u4e2a\u7b26\u5408\u7684\u60c5\u51b5\u90a3\u4e48\u53ef\u80fd\u5bfc\u81f4\u540e\u9762\u6ca1\u529e\u6cd5\u5339\u914d\u3002\u540e\u6765\u60f3\u7684\u662f\u5bbd\u641c\uff0c\u7136\u540e\u8d85\u65f6\uff0c\u4f18\u5316\u4e86\u4e00\u70b9\u8d85\u7a7a\u95f4\u3002\u6240\u4ee5\u8fd9\u9053\u9898\u4e0d\u80fd\u7528\u66b4\u529b\u6765\u5199\u3002\u770b\u4e86\u773c\u9898\u89e3\u3002\u6b63\u89e3\u662fdp+trie\u6811\u3002 \u8f6c\u79fb\u65b9\u7a0bf[i][op] = min(f[i][op], f[j - 1][!op]\u4ee3\u8868\u542b\u4e49\u662f\u76ee\u6807\u4e32s\u7684\u7b2ci\u4e2a\u5b57\u7b26\uff0c\u662f\u5426\u53ef\u7531op\u7684\u76f8\u53cd\u9762\u8f6c\u79fb\u800c\u6765\uff0cj\u662f\u6307\u4ecei\u52301\u8fd9\u4e00\u6bb5\uff0c\u662f\u5426\u5728a\u6216\u8005b\u53d6\u51b3\u4e8e\u5f53\u524dop\u5b58\u5728\u4e00\u4e2a\u4e32\u80fd\u5339\u914dj-i\u8fd9\u4e00\u6bb5\u76ee\u6807\u4e32\u3002 \u96be\u70b9\u662f\u5982\u4f55\u5feb\u901f\u7684\u6c42\u51fa\u662f\u5426\u5b58\u5728\u8fd9\u6837\u7684\u4e00\u4e2a\u5b57\u4e32\u80fd\u591f\u4f7f\u5f97\u8f6c\u79fb\u5230\u5f53\u524d\u72b6\u6001f[i][op],\u7136\u540e\u5c31\u53ef\u4ee5\u7528\u5230trie\u6811\u4e86\u3002\u4e3a\u4e86\u5728\u68c0\u67e5\u7684\u65f6\u5019\u597d\u68c0\u67e5\uff0c\u6211\u4eec\u53ef\u4ee5\u628aa\u7c7b\u548cb\u7c7b\u5efa\u4e00\u4e2a\u4e8c\u7ef4trie\u6811\uff0c\u7136\u540e\u6765\u8ba1\u7b97\uff0c\u5b58\u7684\u65f6\u5019\u53cd\u7740\u5b58\u3002\u68c0\u67e5\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u4ecei,i-1,i-2\uff0c\u65b9\u4fbf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u7684\u4e66\u5199\u3002 \u4ee3\u7801\uff1a #include &lt;algorithm&gt; #include &lt;bitset&gt; #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; #include &lt;fstream&gt; #include &lt;iomanip&gt; #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;queue&gt; #include &lt;set&gt; #include &lt;stack&gt; #include &lt;unordered_map&gt; #include &lt;unordered_set&gt; #include &lt;vector&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[42,18],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-solution","tag-trie","tag-18"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}