{"id":98,"date":"2023-03-28T17:26:37","date_gmt":"2023-03-28T09:26:37","guid":{"rendered":"https:\/\/solost.cn\/?p=98"},"modified":"2023-03-28T17:26:37","modified_gmt":"2023-03-28T09:26:37","slug":"191","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=98","title":{"rendered":"AcWing &#8211; 835. Trie\u5b57\u7b26\u4e32\u7edf\u8ba1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-13.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/www.acwing.com\/problem\/content\/837\/\">\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u89e3<\/h2>\n<pre><code class=\"lang-cpp\">#include&lt;map&gt;\n#include&lt;set&gt;\n#include&lt;stack&gt;\n#include&lt;queue&gt;\n#include&lt;cmath&gt;\n#include&lt;vector&gt;\n#include&lt;bitset&gt;\n#include&lt;cstdio&gt;\n#include&lt;cstring&gt;\n#include&lt;fstream&gt;\n#include&lt;cstdlib&gt;\n#include&lt;iostream&gt;\n#include&lt;algorithm&gt;\n#include&lt;unordered_map&gt;\n\nusing namespace std;\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);\ntypedef long long ll;\ntypedef pair&lt;int,int&gt; PII;\ntypedef unsigned long long ull;\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nint idx;\nint tr[N][30];\nint cnt[N];\nvoid insert(string s){\n    int p = 0;\n    for(auto u : s){\n        u -= &#039;a&#039;;\n        if(!tr[p][u]) tr[p][u] = ++idx;\n        p = tr[p][u];\n    }\n    cnt[p]++;\/\/\u4e0d\u540c\u9898\u76eecnt\u8ba1\u6570\u7684\u4f4d\u7f6e\u4e0d\u540c\uff0c\u56e0\u4e3a\u8fd9\u9053\u9898\u7edf\u8ba1\u7684\u662f\u4ee5\u8fd9\u4e2a\u4e32\u4e3a\u7ed3\u5c3e\u7684\u4e2a\u6570\uff0c\u6240\u4ee5\u5168\u90e8\u52a0\u5b8c\u624d\u4f1a\u8ba1\u6570\n}\n\nint query(string s){\n    int p = 0;\n    for(auto u : s){\n        u -= &#039;a&#039;;\n        if(!tr[p][u]) return 0;\/\/\u5982\u679c\u6b64\u65f6\u8fd8\u6ca1\u6709\u904d\u5386\u5b8c\u5339\u914d\u4e32\u6811\u91cc\u9762\u5c31\u5df2\u7ecf\u6ca1\u6709\u76f8\u5173\u8282\u70b9\u4e86\uff0c\u90a3\u4e48\u5c31\u4e0d\u5b58\u5728\n        p = tr[p][u];\n    }\n    return cnt[p];\n}\n\nvoid solve(){\n    int n;\n    cin &gt;&gt; n;\n    while(n--){\n        char op;\n        string s;\n        cin &gt;&gt; op &gt;&gt; s;\n        if(op == &#039;I&#039;) insert(s);\n        else cout &lt;&lt; query(s) &lt;&lt; endl;\n    }\n}\nint main(){\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\u94fe\u63a5 \u9898\u76ee\u94fe\u63a5 \u9898\u89e3 #include&lt;map&gt; #include&lt;set&gt; #include&lt;stack&gt; #include&lt;queue&gt; #include&lt;cmath&gt; #include&lt;vector&gt; #include&lt;bitset&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;fstream&gt; #include&lt;cstdlib&gt; #include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;unordered_map&gt; using namespace std; #define x first #define y second #define endl &#039;\\n&#039; #define IOS \\ ios_base::sync_with_stdio(0); \\ cin.tie(0); \\ cout.tie(0); typedef long long ll; typedef pair&lt;int,int&gt; PII; typedef unsigned long long ull; const int INF = 0x3f3f3f3f, mod [&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,43],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-solution","tag-trie","tag-43"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/98","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=98"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}