{"id":346,"date":"2020-04-21T14:42:03","date_gmt":"2020-04-21T06:42:03","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=346"},"modified":"2020-05-18T13:35:12","modified_gmt":"2020-05-18T05:35:12","slug":"%e5%9b%9e%e6%96%87%e8%87%aa%e5%8a%a8%e6%9c%ba%e5%8e%9f%e7%90%86-pam","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/21\/%e5%9b%9e%e6%96%87%e8%87%aa%e5%8a%a8%e6%9c%ba%e5%8e%9f%e7%90%86-pam\/","title":{"rendered":"\u56de\u6587\u81ea\u52a8\u673a\u539f\u7406 PAM"},"content":{"rendered":"<h3>\u56de\u6587\u81ea\u52a8\u673a<\/h3>\n<pre><code>\u56de\u6587\u81ea\u52a8\u673a\uff0cPAM(Palindrome Auto Machine)\uff0c\u4e5f\u53eb\u56de\u6587\u6811\uff0c\u662f\u4e00\u4f4d\u6218\u6597\u6c11\u65cf\u7684\u7b97\u6cd5\u4e13\u5bb6Mikhail Rubinchik\u57282015\u5e74\u53d1\u660e\u7684\u4e13\u95e8\u7528\u4e8e\u56de\u6587\u7814\u7a76\u7684\u6570\u636e\u7ed3\u6784\u3002<\/code><\/pre>\n<p>PAM\u7684\u529f\u80fd\u5f88\u5f3a\u5927(\u8bb2\u771f)\u3002\u5b83\u6709\u70b9\u50cfAC\u81ea\u52a8\u673a\uff0c\u5f53\u7136\u4e5f\u662f\u4e00\u4e2a\u81ea\u52a8\u673a\uff0c\u6811\u5f62\u4e0a\u4e5f\u662f\u91c7\u7528\u7684Trie\u6811\u7ed3\u6784\uff0c\u53ea\u662f\u8868\u8fbe\u7684\u610f\u4e49\u548c\u6807\u51c6Trie\u6811\u3001AC\u81ea\u52a8\u673a\u91cc\u7684Trie\u6811\u4e0d\u4e00\u6837\u7f62\u4e86\uff0c\u5728\u540e\u4e24\u8005\u4e2d\uff0cTrie\u6811\u4e0a\u7684\u8fb9\u5b58\u50a8\u7740\u5b57\u7b26\u4fe1\u606f\uff0c\u4ece\u6839\u8282\u70b9\u5230\u4efb\u610f\u8282\u70b9\u8868\u8fbe\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5373\u6cbf\u7740\u67d0\u6761\u8fb9\u5411\u4e0b\u8d70\uff0c\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u540e\u9762\u5c31\u65b0\u52a0\u4e00\u4f4d\u8be5\u8fb9\u4e0a\u5b58\u50a8\u7684\u5b57\u7b26\u3002\u800c\u5728PAM\u4e2d\uff0c<strong>\u6cbf\u7740\u67d0\u6761\u8fb9\u5411\u4e0b\u8d70\uff0c\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u4e24\u8fb9\u90fd\u8981\u52a0\u4e0a\u8be5\u8fb9\u5b58\u50a8\u7684\u5b57\u7b26\uff0c\u5373\u4ece\u6839\u8282\u70b9\u5230\u4efb\u610f\u8282\u70b9\u8868\u8fbe\u7684\u662f\u4e00\u4e2a\u56de\u6587\u5b57\u7b26\u4e32<\/strong>\u3002\u8fd9\u4e2a\u662f\u8981\u77e5\u9053\u7684\uff0c\u5927\u6982\u7684PAM\u5c31\u957f\u8fd9\u6837\u3002<br \/>\n\u56de\u6587\u5b57\u7b26\u4e32\u6709\u4e24\u79cd\uff1a\u957f\u5ea6\u4e3a\u5947\u6570\u548c\u957f\u5ea6\u4e3a\u5076\u6570\u7684\u56de\u6587\u4e32\u3002\u8fd9\u4e24\u4e2a\u7684\u533a\u522b\u4e5f\u5f88\u660e\u663e\uff0c\u6240\u4ee5\u5728PAM\u4e0a\u6709\u4e24\u4e2a\u6839\uff0c\u5206\u522b\u6765\u5b58\u50a8\u4e24\u79cd\u5f62\u5f0f\u4e0a\u4e0d\u540c\u7684\u56de\u6587\u5b57\u7b26\u4e32\u3002\u8fd9\u4e24\u4e2a\u6839\u662f0\u6839\u548c1\u6839(Trie\u53ea\u67090\u6839)\u30020\u6839\u5b58\u50a8\u957f\u5ea6\u4e3a\u5076\u6570\u7684\u56de\u6587\u4e32\uff0c1\u5b58\u50a8\u957f\u5ea6\u4e3a\u5947\u6570\u7684\u56de\u6587\u4e32\u3002<br \/>\n\u7c7b\u4f3c\u4e8eAC\u81ea\u52a8\u673a\uff0cPAM\u4e5f\u6709fail\u8fb9\uff0c\u67d0\u4e2a\u7ed3\u70b9a\u7684fail\u8fb9\u6307\u5411\u7684\u7ed3\u70b9b\u6240\u8868\u8fbe\u7684\u56de\u6587\u4e32\uff0c\u662f\u7ed3\u70b9a\u7684\u6700\u957f\u7684\u56de\u6587\u4e32\u540e\u7f00(\u5982\u56de\u6587\u4e32ababa\uff0c\u5b83\u6709\u4e24\u4e2a\u56de\u6587\u4e32\u540e\u7f00aba\u548ca\uff0caba\u662f\u6700\u957f\u7684)\u3002\u7279\u6b8a\u7684\uff0c0\u6839\u7684fail\u8fb9\u6307\u54111\u6839\uff0c1\u6839\u4e5f\u6307\u54111\u6839(\u5176\u5b9e1\u7684fail\u8fb9\u65e0\u6240\u8c13\uff0c\u4e0d\u540c\u7684\u677f\u5b50\u53ef\u80fd\u4f1a\u4e0d\u4e00\u6837)\u3002<br \/>\n\u5b57\u7b26\u4e32ababaa\u5728PAM\u4e2d\u7684\u5f62\u72b6\u5982\u4e0b\uff1a<br \/>\n<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2020\/05\/PAM.png\" alt=\"\" \/><\/p>\n<h4>\u63a5\u4e0b\u6765\u8bb2\u5982\u4f55\u6784\u9020PAM<\/h4>\n<p>\u4e0d\u540c\u4e8eAC\u81ea\u52a8\u673a\uff0cPAM\u7684fail\u8fb9\u662f\u5728\u7ebf\u6784\u9020\u7684\uff0c\u5373\u6bcf\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\uff0c\u5c31\u8fde\u4e0a\u8fd9\u4e2a\u65b0\u7ed3\u70b9\u7684fail\u8fb9\u3002(AC\u81ea\u52a8\u673a\u662f\u628a\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u63d2\u5165\u540e\u624d\u5f00\u59cb\u8fdefail\u8fb9\uff0c\u4e14\u4e0d\u80fd\u4fee\u6539)<br \/>\n\u58f0\u660e\u51e0\u4e2a\u7528\u5230\u7684\u53d8\u91cf\uff1a<\/p>\n<blockquote>\n<p><strong>Trie[][27]\uff1a\u7528\u6765\u5b58\u50a8\u6240\u6709\u4fe1\u606f\u7684\u6811\u7ed3\u6784<br \/>\nsize\uff1aTrie\u4e2d\u5df2\u7ecf\u7528\u4e86\u7684\u7ed3\u70b9\u4e2a\u6570\u3002<br \/>\nmp[i]\uff1a\u5b57\u7b26\u4e32\u4e2d\u4e0b\u6807\u4e3ai\u7684\u5b57\u7b26\u5bf9\u5e94\u4e8eTrie\u6811\u4e2d\u54ea\u4e00\u4e2a\u7ed3\u70b9<br \/>\nfail[j]\uff1afail\u8fb9\uff0cj\u7ed3\u70b9\u6240\u8868\u793a\u7684\u56de\u6587\u4e32\u7684\u6700\u5927\u56de\u6587\u540e\u7f00(fail[j]\u7ed3\u70b9\u6240\u8868\u793a\u7684\u56de\u6587\u4e32)<br \/>\nRoot0\uff1a0\u6839<br \/>\nRoot1\uff1a1\u6839<br \/>\nlast\uff1a\u6700\u65b0\u63d2\u5165\u7684\u7ed3\u70b9\u7684\u4f4d\u7f6e<br \/>\nLen[j]\uff1a\u7ed3\u70b9j\u5bf9\u5e94\u7684\u56de\u6587\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<br \/>\ncnt[j]\uff1a\u7ed3\u70b9j\u5bf9\u5e94\u7684\u56de\u6587\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e0d\u540c\u7684\u56de\u6587\u4e32\u540e\u7f00\u7684\u4e2a\u6570\u3002<br \/>\nHalf[i]\uff1aHalf[i]\u8868\u793a\u7ed3\u70b9i\u7684\u957f\u5ea6\u4e0d\u8d85\u8fc7Len[i]\/2\u7684\u6700\u5927\u56de\u6587\u540e\u7f00<\/strong><\/p>\n<\/blockquote>\n<p>\u6700\u540e1\u4e2a\u662f\u6839\u636e\u5b9e\u9645\u9700\u8981\u65f6\u8981\u8bbe\u7acb\u7684\u4e00\u4e9b\u53d8\u91cf\uff0c\u524d\u9762\u7684\u53d8\u91cf\u662fPAM\u6240\u5fc5\u9700\u7684\u3002\u56e0\u6b64\u5148\u8bb2\u524d\u9762\u7684\u51e0\u4e2a\u53d8\u91cf\u5982\u4f55\u6784\u9020\u4e0e\u8d4b\u503c\u3002<br \/>\n<code class=\"katex-inline\">Len[i] = Len[last] + 2<\/code><br \/>\n<code class=\"katex-inline\">Cnt[i] = Cnt[fail[i]] + 1<\/code><br \/>\nhalf\u6307\u9488\u8981\u5728i\u7684\u7236\u4eb2\u7ed3\u70b9\u7684half\u6307\u9488\u7684fail\u94fe\u91cc\u9762\u5bfb\u627e\u3002<br \/>\n\u5f53Len[i]&lt;=2\u65f6\uff0chalf=fail\u3002<br \/>\n\u9700\u8981\u5b9e\u65f6\u66f4\u65b0\u7684\u53d8\u91cf\u6709last\uff0cmp[i]\u4f60\u5f97\u6620\u5c04\u597d\uff0cfail\u8fb9\u5f97\u8fde\u4e0a\uff0ccnt\u5f97\u5f04\u597d\uff0c\u8fd8\u6709half\u3002<\/p>\n<p>\u677f\u5b50<\/p>\n<pre><code class=\"language-c++\">typedef struct PAM{\n    int size,last,root0,root1,Trie[maxn][27],fail[maxn];\n    int Len[maxn],mp[maxn],cnt[maxn];\n    PAM(){\n        size = 2;\n        root0 = 0, root1 = 1, last = root1;\n        Len[root0] = 0, fail[root0] = root1;\n        Len[root1] = -1, fail[root1] = root1;\n    }\n    void Extend(char ch,int idx){\n        while (str[idx-1-Len[last]]!=str[idx]) last = fail[last];\n        if (!Trie[last][ch-&#039;a&#039;+1]){\n            int v = fail[last];\n            while (str[idx-1-Len[v]]!=str[idx]) v = fail[v];\n            \/\/\u8fd9\u91cc\u8fd9\u4e2a\u5730\u65b9\u5f88\u91cd\u8981\uff0c\u5148\u8fdefail\u8fb9\uff0c\u518d\u7ed9Trie\u4e0a\u6dfb\u52a0\u65b0\u7684\u70b9\uff0c\u4e3a\u9632\u6b62fail\u6307\u5411\u81ea\u5df1\uff01\uff01\n            size++;\n            fail[size] = Trie[v][ch-&#039;a&#039;+1];\n            Trie[last][ch-&#039;a&#039;+1] = size;\n            \/\/\n            Len[size] = Len[last] + 2;\n            cnt[size] = cnt[fail[size]] + 1;\n            \/\/\n            if (Len[size]&lt;=2)   trans[size] = fail[size];\n            else{\n                int tp = trans[last];\n                while (s[idx-Len[tp]-1]!=s[idx] || Len[tp]+2&gt;Len[size]\/2) tp = fail[tp];\n                trans[size] = Trie[tp][ch-&#039;a&#039;+1];\n            }\n        }\n        mp[idx] = Trie[last][ch-&#039;a&#039;+1];\n        last = Trie[last][ch-&#039;a&#039;+1];\n    }\n    void Build(char *str){\n        int len = strlen(str+1);\n        for (int i=1;i&lt;=len;i++)\n            Extend(str[i],i);\n    }\n}PAM;<\/code><\/pre>\n<h4>\u5e94\u7528<\/h4>\n<p>1.\u6c42\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u672c\u8d28\u4e0d\u540c\u7684\u56de\u6587\u4e32\u7684\u4e2a\u6570\u3002<br \/>\n\u6211\u4eec\u77e5\u9053\u5728Tire\u6811\u4e2d\u4e0d\u4f1a\u6709\u4e24\u4e2a\u5b57\u7b26\u4e32\u91cd\u590d\u7684\uff0c\u6240\u4ee5\u5728PAM\u91cc\u7684Trie\u4e2d\uff0c\u9664\u4e860\u6839\u548c1\u6839\u5916\uff0c\u5269\u4e0b\u7684\u7ed3\u70b9\u90fd\u552f\u4e00\u8868\u793a\u4e00\u4e2a\u56de\u6587\u4e32\uff0c\u5373\u672c\u8d28\u4e0d\u540c\u7684\u56de\u6587\u4e32\u6709size-1\u4e2a\u3002<br \/>\n2.\u7edf\u8ba1\u6bcf\u4e2a\u56de\u6587\u4e32\u51fa\u73b0\u7684\u6b21\u6570\u3002<a href=\"https:\/\/www.luogu.com.cn\/problem\/P3649\">\u6d1b\u8c37P3649 \u56de\u6587\u4e32<\/a><br \/>\n\u8dd1\u4e00\u904dfail\u6811\uff0c\u7c7b\u4f3c\u4e8eAC\u81ea\u52a8\u673a\u4e86\u3002\u9700\u8981\u7528\u5230\u62d3\u6251\u6392\u5e8f<\/p>\n<p>\u4f8b\u9898<br \/>\n<a href=\"https:\/\/www.luogu.com.cn\/problem\/P3649\">\u6d1b\u8c37P3649 \u56de\u6587\u4e32<\/a><br \/>\n\u6784\u9020PAM\uff0c\u56de\u6587\u81ea\u52a8\u673a\uff0c\u7136\u540e\u5bf9fail\u6811\u62d3\u6251\u6392\u5e8f\uff0c\u6700\u540e\u8f93\u51fa\u7b54\u6848\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\nconst int maxn = 3e5+50;\nchar str[maxn];\ntypedef struct PAM{\n    int size,last,root0,root1,Trie[maxn][27],fail[maxn];\n    int Len[maxn],mp[maxn],cnt[maxn],times[maxn],in[maxn];\n    PAM(){\n        size = 1;\n        root0 = 0, root1 = 1, last = root1;\n        Len[root0] = 0, fail[root0] = root1;\n        Len[root1] = -1, fail[root1] = root1;\n    }\n    void Extend(char ch,int idx){\n        while (str[idx-1-Len[last]]!=str[idx]) last = fail[last];\n        if (!Trie[last][ch-&#039;a&#039;+1]){\n            int v = fail[last];\n            while (str[idx-1-Len[v]]!=str[idx]) v = fail[v];\n            fail[++size] = Trie[v][ch-&#039;a&#039;+1];               \/\/\u5fc5\u987b\u8981\u5148\u8fdefail\u8fb9\n            Trie[last][ch-&#039;a&#039;+1] = size;\n            Len[size] = Len[last] + 2;\n            cnt[size] = cnt[fail[size]] + 1;\n            in[fail[size]]++;\n        }\n        mp[idx] = Trie[last][ch-&#039;a&#039;+1];\n        last = Trie[last][ch-&#039;a&#039;+1];\n        times[mp[idx]]++;\n    }\n    void Build(char *str,int len){\n        for (int i=1;i&lt;=len;i++)\n            Extend(str[i],i);\n        topu();\n        return;\n    }\n    void topu(){\n        queue&lt;int&gt; q;\n        for (int i=2;i&lt;=size;i++)   if (in[i]==0)\n            q.push(i);\n        while (!q.empty()){\n            int 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}PAM;\nPAM P;\nint main(){\n    \/\/freopen(&quot;P3649_8.txt&quot;,&quot;r&quot;,stdin);\n    ll len,ans;\n    scanf (&quot;%s&quot;,str+1);\n    len = strlen(str+1);    ans = 0;\n    P.Build(str,(int)len);\n    for (int i=2;i&lt;=P.size;i++)\n        ans = max(ans,(ll)P.times[i] * P.Len[i]);\n    printf (&quot;%lld&quot;,ans);\n    return 0;\n}<\/code><\/pre>\n<h4><a href=\"https:\/\/www.luogu.com.cn\/problem\/P4287\">\u6d1b\u8c37 P4287 \u53cc\u500d\u56de\u6587<\/a><\/h4>\n<p>\u628a0\u6839\u91cc\u7684\uff0c\u957f\u5ea6\u4e3a4\u7684\u500d\u6570\u7684\u56de\u6587\u4e32\u548c\u5b83\u7684half\u56de\u6587\u6bd4\u8f83\uff0c\u5224\u65ad\u540e\u8005\u662f\u5426\u662f\u524d\u8005\u7684\u4e00\u534a\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\n#include &lt;queue&gt;\n#include &lt;vector&gt;\n#include &lt;utility&gt;\nusing namespace std;\nconst int maxn = 5e5+5;\nint len;\nchar s[maxn];\ntypedef struct PAM{\n    int Trie[maxn][27],fail[maxn],size,Len[maxn],last;\n    int ans,half[maxn];\n    PAM(){\n        fail[0] = fail[1] = 1;\n        Len[0] = 0,Len[1] = -1;\n        size = 1;   last = 1;\n        ans = 0;\n    }\n    void Insert(char ch,int idx){\n        while (s[idx-1-Len[last]]!=s[idx])  last = fail[last];\n        if (!Trie[last][ch-&#039;a&#039;+1]){\n            int v = fail[last];\n            while (s[idx-Len[v]-1]!=s[idx]) v = fail[v];\n            fail[++size] = Trie[v][ch-&#039;a&#039;+1];\n            Trie[last][ch-&#039;a&#039;+1] = size;\n            Len[size] = Len[last] + 2;\n            \/\/\n            if (Len[size]&lt;=2)   half[size] = fail[size];\n            else{\n                int tp = half[last];\n                while (s[idx-Len[tp]-1]!=s[idx] || Len[tp]+2&gt;Len[size]\/2) tp = fail[tp];\n                half[size] = Trie[tp][ch-&#039;a&#039;+1];\n            }\n        }\n        last = Trie[last][ch-&#039;a&#039;+1];\n    }\n    void Build(char *s,int len){\n        for (int i=1;i&lt;=len;i++){\n            Insert(s[i],i);\n        }\n        queue&lt;int&gt; q;\n        for (int i=1;i&lt;=26;i++) if (Trie[0][i])\n            q.push(Trie[0][i]);\n        while (!q.empty()){\n            int tp = q.front(); q.pop();\n            if (Len[tp]%4==0 &amp;&amp; Len[half[tp]]==Len[tp]\/2) ans = max(ans,Len[tp]);\n            for (int i=1;i&lt;=26;i++) if (Trie[tp][i])\n                q.push(Trie[tp][i]);\n        }\n        return;\n    }\n}PAM;\nPAM P;\nint main(){\n    scanf (&quot;%d %s&quot;,&amp;len,s+1);\n    P.Build(s,len);\n    printf (&quot;%d\\n&quot;,P.ans);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u56de\u6587\u81ea\u52a8\u673a \u56de\u6587\u81ea\u52a8\u673a\uff0cPAM(Palindrome Auto Machine)\uff0c\u4e5f\u53eb\u56de\u6587\u6811\uff0c\u662f\u4e00\u4f4d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/346"}],"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=346"}],"version-history":[{"count":24,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":349,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/346\/revisions\/349"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}