{"id":77,"date":"2020-03-26T17:56:14","date_gmt":"2020-03-26T09:56:14","guid":{"rendered":"http:\/\/47.103.223.234:100\/?p=77"},"modified":"2020-04-21T22:21:19","modified_gmt":"2020-04-21T14:21:19","slug":"ac%e8%87%aa%e5%8a%a8%e6%9c%ba%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/03\/26\/ac%e8%87%aa%e5%8a%a8%e6%9c%ba%e8%a7%a3%e6%9e%90\/","title":{"rendered":"AC\u81ea\u52a8\u673a\u89e3\u6790"},"content":{"rendered":"<p>AC\u81ea\u52a8\u673a\u7528\u4e8e\u5728\u4e3b\u4e32\u4e2d\uff0c\u627e\u591a\u4e2a\u6a21\u677f\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4e0d\u8bba\u662f\u51fa\u73b0\u6b21\u6570\u53ea\u8bb01\u6b21\uff0c\u8fd8\u662f\u51e0\u591a\u6b21\uff0c\u90fd\u53ef\u3002<br \/>\nAC\u81ea\u52a8\u673a\u5176\u5b9e\u5c31\u662f\u6a21\u677f\u4e32\u6784\u6210\u7684Trie\u56fe\u52a0\u4e0afail\u8fb9\uff0c\u6709\u70b9\u50cfkmp\u91cc\u7684Next\u6570\u7ec4\uff0c\u5177\u4f53\u89e3\u6790\u5148\u653e\u4e0b\uff0c\u6ca1\u65f6\u95f4\uff0c\u4e0b\u9762\u662f\u677f\u5b50\u3002<\/p>\n<p>e\u5bf9\u4e86\uff0c\u6700\u8fd1\u7684\u4e1c\u897f\u786e\u5b9e\u6709\u96be\u5ea6\uff0c\u7136\u989d\u5bf9\u4e8e\u7a0d\u5fae\u96be\u4e00\u70b9\u7684\u4e1c\u897f\uff0c\u7f51\u4e0a\u7684\u677f\u5b50\u90fd\u662f\u90a3\u79cd\u53c2\u5dee\u4e0d\u9f50\u7684\uff0c\u4e71\u8bbe\u53d8\u91cf\uff0c\u5199\u7684\u53ea\u6709\u81ea\u5df1\u770b\u5f97\u61c2\uff0c\u6b63\u786e\u6027\u8fd8\u4e0d\u7edd\u5bf9\u4fdd\u8bc1\uff0c\u53d8\u91cf\u4e5f\u4e0d\u8bf4\u660e\u90fd\u662f\u5e72\u5565\u7528\u7684\uff0c\u770b\u7684\u6c14\u6124\u3002\u3002<br \/>\n\u6240\u4ee5\uff0c\u6700\u91cd\u8981\u7684\u8fd8\u662f\u5148\u89e3\u91ca\u4ee3\u7801\u4e2d\u7684\u53d8\u91cf\u90fd\u662f\u5e72\u5565\u7528\u7684\uff0c\u5565\u529f\u80fd\uff0c\u540d\u5b57\u5c3d\u53ef\u80fd\u89c4\u8303\u5427\u3002<br \/>\nTrie\u7528\u6765\u5b58\u6811(\u56fe)\uff0cfail[i]\u8868\u793a\u7f16\u53f7\u4e3ai\u7684\u7ed3\u70b9\u6307\u5411\u7684\u7ed3\u70b9\uff0cval[i]\u8868\u793a\u7f16\u53f7\u4e3ai\u7684\u7ed3\u70b9\u662f\u51e0\u4e2a\u5b57\u7b26\u4e32\u7684\u7ec8\u70b9\u3002cnt\u8868\u793aTrie\u6811\u4e2d\u5df2\u7ecf\u6709\u591a\u5c11\u4e2a\u7ed3\u70b9\u4e86\u3002\u8fd9\u91cc\u518d\u8bf4\u660e\u4e00\u4e0b\uff0cTrie\u6811\u6b63\u89e3\u5e94\u8be5\u662f\u6570\u636e(\u5b57\u7b26)\u90fd\u662f\u5728\u8fb9\u4e0a\u5b58\u7684\uff0c\u70b9\u7684\u503c\u4e3a\u7f16\u53f7\u3002<\/p>\n<pre><code class=\"language-c++\">typedef struct AC_Auto{\n    int Trie[maxn][26],fail[maxn],val[maxn],cnt;\n    void insert(char *s,int len);    \/\/\u8981\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u957f\u5ea6\n    void build();                    \/\/\u6784\u5efa\u81ea\u52a8\u673a\uff0c\u4e0d\u9700\u8981\u53c2\u6570\n    int query(char *s,int len);     \/\/\u6839\u636e\u5b9e\u9645\u9700\u8981\u5199\n    void clear();\n}AC;\nvoid AC::insert(char *s,int len){\n    int root = 0;                   \/\/\u63d2\u5165\u5f53\u7136\u662f\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u5566\n    for (int i=1;i&lt;=len;i++){\n        if (!Trie[root][s[i]-&#039;a&#039;+1])  Trie[root][s[i]-&#039;a&#039;+1] = ++cnt;\n        root = Trie[root][s[i]-&#039;a&#039;+1];\n    }\n    val[root]++;                  \/\/\u7ec8\u6b62\u7ed3\u70b9\u6807\u6ce8\u4e00\u4e0b\u3002\n    return;\n}\nvoid AC::build(){\n    queue&lt;int&gt; q;\n    for (int i=1;i&lt;=26;i++)  if (Trie[0][i])  \/\/\u4e0e\u6839\u8282\u70b9\u76f4\u63a5\u76f8\u8fde\u7684\u7ed3\u70b9fail\u8fb9\u90fd\u662f\u76f4\u63a5\u6307\u5411\u6839\u7684\uff0c\u90a3\u4e48\u5165\u961f\u5c31\u884c\n        q.push(Trie[0][i]);\n    while (!q.empty()){\n        int temp = q.front();   q.pop();\n        for (int i=1;i&lt;=26;i++){\n            if (Trie[temp][i]){\n                fail[Trie[temp][i]] = Trie[fail[temp]][i];\n                q.push(Trie[temp][i]);\n            }else{\n                Trie[temp][i] = Trie[fail[temp]][i];\n            }\n        }\n    }\n    return;\n}\nint AC::query(char *s,int len){\n    int ans = 0;\n    int root = 0,temp;\n    for (int i=1;i&lt;=len;i++){         \/\/i\u662f\u626b\u63cf\u4e3b\u4e32\u7684\uff0croot\u662f\u626b\u63cfTrie\u56fe\u7684\uff0c\u4e0ekmp\u4e5f\u76f8\u4f3c\u3002\u4e00\u4e2a\u626b\u63cf\u4e3b\u4e32\uff0c\u4e00\u4e2a\u626b\u63cf\u6a21\u677f\u4e32\u3002\n        root = Trie[root][s[i]-&#039;a&#039;+1];\n        temp = root;\n        while (root){\n            ans += val[root];\n            root = fail[root];\n        }\n    }\n    return ans;\n}\nvoid AC::clear(){\n    cnt = 0;\n    memset(Trie,0,sizeof(Trie));\n    memset(fail,0,sizeof(fail));\n    memset(val,0,sizeof(val));\n}\n<\/code><\/pre>\n<p>AC\u81ea\u52a8\u673a\u4e0d\u652f\u6301\u5728\u7ebf\u64cd\u4f5c\uff0c\u5982\u679c\u4e2d\u9014\u63d2\u5165\u5176\u4ed6\u7684\u4e32\uff0c\u5c31\u5fc5\u987b\u518d\u6b21build\u3002<\/p>\n<p>emmmm..\u677f\u5b50\u786e\u5b9e\u957f\uff0c\u5728\u6ce8\u610f\u51e0\u70b9\uff0c\u4e0a\u9762\u5728\u8fdefail\u8fb9\u7684\u65f6\u5019\uff0c\u82e5root\u7684\u67d0\u4e2a\u5b50\u7ed3\u70b9\u5b58\u5728\uff0c\u90a3\u4e48fail\u8fb9\u76f4\u63a5\u8fde\u4e0aroot\u7684fail\u7684\u5b50\u7ed3\u70b9\uff0c\u7136\u540e\u5165\u961f\uff1b\u82e5\u4e0d\u5b58\u5728\uff0c\u8fd9\u91cc\u4f18\u5316\u4e00\u4e0b\uff0c\u4e0d\u5b58\u5728\u5c31\u4e0d\u8fdefail\u8fb9\u4e86\uff0c\u76f4\u63a5\u628aTrie[root][s[i]-'a'+1]\u8d4b\u503c\u4e3aroot\u7684fail\u7684\u5b50\u7ed3\u70b9\uff0c\u8fd9\u6837\uff0c\u5728\u5339\u914d\u4e3b\u4e32\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f53\u6210\u8be5\u5b50\u7ed3\u70b9\u5b58\u5728\u53bb\u5339\u914d\uff0c\u4e0equery()\u4e2d\u7684root = Trie[root][s[i]-'a'+1];\u662f\u5bf9\u4e0a\u7684\uff0c\u8fd9\u91cc\u8981\u518d\u597d\u597d\u60f3\u60f3\uff01<\/p>\n<p>\u8865\u5145\uff1a\u8fd9\u4e2a\u677f\u5b50\u7684query\u662f\u8ba1\u7b97\u6bcf\u4e2a\u6a21\u677f\u4e32\u51fa\u73b0\u591a\u6b21\u7684\u677f\u5b50\u3002<\/p>\n<p>\u6700\u65b0\u7684\u677f\u5b50(topu\u6392\u5e8f)<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P5357\">\u6d1b\u8c37P5357 AC\u81ea\u52a8\u673a \u4e8c\u6b21\u52a0\u5f3a\u7248<\/a><\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;cstring&gt;\n#include &lt;queue&gt;\nusing namespace std;\nconst int maxn = 2e5+5,maxn2 = 2e6+5;\ntypedef struct AC_Auto{\n    int Trie[maxn][26],fail[maxn],val[maxn],cnt;\n    int ans[maxn],cnt2,mp[maxn],in[maxn];\n    int times[maxn];                  \/\/\u8868\u793a\u67d0\u4e2a\u7ed3\u70b9\u51fa\u73b0\u4e86\u591a\u5c11\u6b21\n    AC_Auto(){\n        cnt = cnt2 = 0;\n        memset (times,0,sizeof times);\n    }\n    void insert(char *s,int len,int flag);    \/\/\u8981\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u4ee5\u53ca\u957f\u5ea6\n    void build();                    \/\/\u6784\u5efa\u81ea\u52a8\u673a\uff0c\u4e0d\u9700\u8981\u53c2\u6570\n    int query(char *s,int len);     \/\/\u6839\u636e\u5b9e\u9645\u9700\u8981\u5199\n    void clear();\n    void output();\n    void topu();\n}AC;\nchar s[maxn2];\nint n,len;\nAC a;\nint main(){\n    scanf (&quot;%d&quot;,&amp;a.cnt2);\n    for (int i=1;i&lt;=a.cnt2;i++){\n        scanf (&quot;%s&quot;,s+1);\n        a.insert(s,strlen(s+1),i);\n    }\n    scanf (&quot;%s&quot;,s+1);\n    a.build();\n    a.query(s,strlen(s+1));\n    a.topu();\n    a.output();\n    return 0;\n}\n\nvoid AC::insert(char *s,int len,int flag){\n    int root = 0;                   \/\/\u63d2\u5165\u5f53\u7136\u662f\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u5566\n    for (int i=1;i&lt;=len;i++){\n        if (!Trie[root][s[i]-&#039;a&#039;+1])  Trie[root][s[i]-&#039;a&#039;+1] = ++cnt;\n        root = Trie[root][s[i]-&#039;a&#039;+1];\n    }\n    val[root]++;                  \/\/\u7ec8\u6b62\u7ed3\u70b9\u6807\u6ce8\u4e00\u4e0b\u3002\n    mp[flag] = root;\n    return;\n}\nvoid AC::build(){\n    queue&lt;int&gt; q;\n    for (int i=1;i&lt;=26;i++)  if (Trie[0][i])  \/\/\u4e0e\u6839\u8282\u70b9\u76f4\u63a5\u76f8\u8fde\u7684\u7ed3\u70b9fail\u8fb9\u90fd\u662f\u76f4\u63a5\u6307\u5411\u6839\u7684\uff0c\u90a3\u4e48\u5165\u961f\u5c31\u884c\n        q.push(Trie[0][i]);\n    while (!q.empty()){                       \/\/\u4ece\u961f\u5217\u91cc\u9762\u62ff\u51fa\u6765\u4e00\u4e2a\u5143\u7d20temp\uff0c\u7136\u540e\u5f00\u59cb\u5bf9temp\u7684\u5b69\u5b50\u7ed3\u70b9\u8fdefail\u8fb9\n        int temp = q.front();   q.pop();\n        for (int i=1;i&lt;=26;i++){\n            if (Trie[temp][i]){\n                fail[Trie[temp][i]] = Trie[fail[temp]][i];\n                in[Trie[fail[temp]][i]]++;\n                q.push(Trie[temp][i]);\n            }else{\n                Trie[temp][i] = Trie[fail[temp]][i];\n            }\n        }\n    }\n    return;\n}\nint AC::query(char *s,int len){\n    int root = 0,temp;\n    for (int i=1;i&lt;=len;i++){         \/\/i\u662f\u626b\u63cf\u4e3b\u4e32\u7684\uff0croot\u662f\u626b\u63cfTrie\u56fe\u7684\uff0c\u4e0ekmp\u4e5f\u76f8\u4f3c\u3002\u4e00\u4e2a\u626b\u63cf\u4e3b\u4e32\uff0c\u4e00\u4e2a\u626b\u63cf\u6a21\u677f\u4e32\u3002\n        root = Trie[root][s[i]-&#039;a&#039;+1];\n        times[root]++;\n    }\n    return 0;\n}\nvoid AC::topu(){\n    queue&lt;int&gt; q;\n    int temp;\n    for (int i=1;i&lt;=cnt;i++)    if (in[i]==0)\n        q.push(i);\n    while (!q.empty()){\n        temp = q.front();   q.pop();\n        times[fail[temp]] += times[temp];\n        in[fail[temp]]--;\n        if (in[fail[temp]]==0)\n            q.push(fail[temp]);\n    }\n    return;\n}\n\nvoid AC::output(){\n    for (int i=1;i&lt;=cnt2;i++)\n        printf (&quot;%d\\n&quot;,times[mp[i]]);\n    return;\n}\nvoid AC::clear(){\n    cnt = cnt2 = 0;\n    memset(Trie,0,sizeof(Trie));\n    memset(fail,0,sizeof(fail));\n    memset(val,0,sizeof(val));\n    memset(ans,0,sizeof(ans));\n    memset(mp,0,sizeof(mp));\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AC\u81ea\u52a8\u673a\u7528\u4e8e\u5728\u4e3b\u4e32\u4e2d\uff0c\u627e\u591a\u4e2a\u6a21\u677f\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4e0d\u8bba\u662f\u51fa\u73b0\u6b21\u6570\u53ea\u8bb01\u6b21\uff0c\u8fd8\u662f\u51e0\u591a\u6b21\uff0c\u90fd\u53ef\u3002 AC\u81ea\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":7,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":375,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions\/375"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}