{"id":385,"date":"2020-04-24T21:35:17","date_gmt":"2020-04-24T13:35:17","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=385"},"modified":"2020-05-18T13:53:43","modified_gmt":"2020-05-18T05:53:43","slug":"%e5%9b%9e%e6%96%87%e8%87%aa%e5%8a%a8%e6%9c%bapam-%e4%be%8b%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/04\/24\/%e5%9b%9e%e6%96%87%e8%87%aa%e5%8a%a8%e6%9c%bapam-%e4%be%8b%e9%a2%98\/","title":{"rendered":"\u56de\u6587\u81ea\u52a8\u673aPAM \u4f8b\u9898"},"content":{"rendered":"<h4><a href=\"https:\/\/www.luogu.com.cn\/problem\/P4555\">\u6d1b\u8c37 P4555 \u6700\u957f\u53cc\u56de\u6587\u4e32<\/a><\/h4>\n<p>\u8f93\u5165\u957f\u5ea6\u4e3an\u7684\u4e32S\uff0c\u6c42S\u7684\u6700\u957f\u53cc\u56de\u6587\u5b50\u4e32T,\u5373\u53ef\u5c06T\u5206\u4e3a\u4e24\u90e8\u5206X\uff0cY\uff0c\uff08|X|,|Y|\u22651\uff09\u4e14X\u548cY\u90fd\u662f\u56de\u6587\u4e32\u3002<\/p>\n<p>\u5206\u6790\uff1a\u6784\u9020\u4e24\u4e2a\u56de\u6587\u81ea\u52a8\u673aPAM\uff0c\u628a\u4e32s\u6b63\u53cd\u5206\u522b\u8dd1\u4e00\u904d\uff0c\u7136\u540e\u679a\u4e3e\u5206\u5272\u70b9\u5c31\u884c\u4e86\u3002<br \/>\n\u5f88\u5de7\u5999\u5730\u4e00\u4e2a\u4f5c\u6cd5\uff0c\u679a\u4e3e\u5206\u5272\u70b9\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\n#include &lt;cstring&gt;\nusing namespace std;\nconst int maxn = 1e5+5;\nint len;\nchar s[maxn];\ntypedef struct PAM{\n    int Trie[maxn][27],fail[maxn],size,Len[maxn],last,mp[maxn];\n    int ans;\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        last = Trie[last][ch-&#039;a&#039;+1];\n        mp[idx] = last;\n    }\n    void Build(char *s,int len){\n        for (int i=1;i&lt;=len;i++){\n            Insert(s[i],i);\n        }\n        return;\n    }\n}PAM;\nPAM A,B;\nint ans = 0;\nvoid solve(){\n    for (int i=1;i&lt;len;i++)\n        ans = max(ans,A.Len[A.mp[i]]+B.Len[B.mp[len-i]]);\n    printf (&quot;%d&quot;,ans);\n    return;\n}\nint main(){\n    scanf (&quot;%s&quot;,s+1);\n    len = strlen(s+1);\n    A.Build(s,len);\n    for (int i=1;i&lt;=len\/2;i++)  swap(s[i],s[len-i+1]);\n    B.Build(s,len);\n    solve();\n    return 0;\n}<\/code><\/pre>\n<h4><a href=\"https:\/\/www.luogu.com.cn\/problem\/P6216\">\u6d1b\u8c37P6216 \u56de\u6587\u5339\u914d<\/a><\/h4>\n<p>\u7ed9\u5b9a\u4e24\u5b57\u7b26\u4e32s1, s2.\u6c42s2\u5728(s1\u4e2d\u7684\u6240\u6709\u5947\u6570\u957f\u5ea6\u56de\u6587\u4e2d)\u7684\u51fa\u73b0\u6b21\u6570\u4e4b\u548c\u3002\u5947\u6570\u56de\u6587\u6709\u76f8\u540c\u7684\uff0c\u6309\u591a\u6b21\u8ba1\u7b97\u3002<br \/>\n<strong>PAM+KMP+TOPO<\/strong><br \/>\n\u6570\u636e\u91cf\u5de8\u5927\uff0c\u51fa\u9898\u4eba\u4f30\u8ba1\u60f3\u5361PAM\uff0c\uff0c\uff0c\u4f46\u6211\u8fd8\u662f\u5f3a\u884cA\u4e86<br \/>\n\u4e5f\u633a\u7b80\u5355\uff0c\uff0c\u57fa\u672c\u6b65\u9aa4\uff1a<br \/>\n1.\u5bf9s1\u6784\u5efaPAM\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u5bf9\u5e94\u7684\u56de\u6587\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u7528TOPO\u89e3\u51b3\u3002<br \/>\n2.s2\u5728\u6bcf\u4e2a\u5947\u6570\u56de\u6587\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u7528KMP\u89e3\u51b3\u3002<br \/>\n\u7136\u540e\u6700\u540e\u7b54\u6848\uff0c\u5c31\u662f(\u6bcf\u4e2a\u5947\u6570\u56de\u6587\u7684\u51fa\u73b0\u6b21\u6570\u4e58\u4ee5s2\u5728\u8be5\u5947\u6570\u56de\u6587\u4e2d\u7684\u51fa\u73b0\u6b21\u6570)\u4e4b\u548c\u4e86\u3002<br \/>\n\u5176\u4e2d\u6c42\u7b2c2\u6b65\u65f6\uff0c\u5e94\u5148\u5728s1\u4e0a\u8dd1\u4e00\u904dKMP\uff0c\u6c42\u51fa\u533a\u95f4[1,i]\u5185s2\u7684\u51fa\u73b0\u6b21\u6570(\u5373\u524d\u7f00\u548c)\uff0c\u7136\u540e\u5bf9PAM\u4e2d\u7684\u6bcf\u4e2a\u7ed3\u70b9\uff0c\u5224\u65ad\u5176\u5bf9\u5e94\u7684\u56de\u6587\u5728s1\u4e2d\u7684\u533a\u95f4\uff0c\u76f4\u63a5O(1)\u6c42\u51fas2\u5728\u8be5\u56de\u6587\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002<br \/>\n\u6c42\u524d\u7f00\u548c\uff0cs2\u5728s1\u4e2d\u51fa\u73b0\u7684\u7ec8\u7ed3\u70b9\u8bb0\u4e3as2\u7684\u51fa\u73b0\u4f4d\u7f6e\u3002<br \/>\n<strong>\u65f6\u95f4\u590d\u6742\u5ea6O(n+m)\uff0c\u5e38\u6570\u5f88\u5927\uff0c\u57fa\u672c\u628a\u65f6\u95f4\u590d\u6742\u5ea6\u5411\u4e0a\u7ffb\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002<\/strong><\/p>\n<pre><code class=\"language-c++\">#include &lt;cstdio&gt;\n#include &lt;algorithm&gt;\n#include &lt;queue&gt;\n#include &lt;cstring&gt;\n#include &lt;cmath&gt;\nusing namespace std;\ntypedef long long ll;\nconst int maxn = 3e6+5;\ntypedef struct PAM{\n    int Trie[maxn][27],fail[maxn],size,last,Len[maxn],in[maxn];\n    int times[maxn];\n    int len1,len2;\n    char s1[maxn],s2[maxn];\n    int Next[maxn],mark[maxn];               \/\/mark\u6807\u8bb0\u7ed3\u70b9\u5728\u6bcd\u4e32s1\u4e2d\u7684\u4f4d\u7f6e\n    ll dp[maxn];\n    ll ans,mod;\n    PAM(){\n        last = size = 1;\n        Len[0] = 0,Len[1] = -1;\n        fail[0] = fail[1] = 1;\n        ans = 0;\n        mod = pow(2, 32);\n    }\n    void Insert(char ch,int idx){\n        while (s1[idx-Len[last]-1]!=s1[idx])  last = fail[last];\n        if (!Trie[last][ch-&#039;a&#039;+1]){\n            int v = fail[last];\n            while (s1[idx-Len[v]-1]!=s1[idx])   v = fail[v];\n            fail[++size] = Trie[v][ch-&#039;a&#039;+1];\n            in[fail[size]]++;\n            Trie[last][ch-&#039;a&#039;+1] = size;\n            Len[size] = Len[last] + 2;\n        }\n        times[Trie[last][ch-&#039;a&#039;+1]]++;\n        last = Trie[last][ch-&#039;a&#039;+1];\n        if (!mark[last])    mark[last] = idx;\n    }\n    void Build(){\n        for (int i=1;i&lt;=len1;i++)\n            Insert(s1[i],i);\n        topu();\n        printf (&quot;%lld&quot;, ans);\n        return;\n    }\n    void KMP(){\n        for (int i=2,j=0;i&lt;=len2;i++){\n            while (j &amp;&amp; s2[i]!=s2[j+1])    j = Next[j];\n            if (s2[i]==s2[j+1])   j++;\n            Next[i] = j;\n        }\n        for (int i=1,j=0;i&lt;=len1;i++){\n            while (j &amp;&amp; s1[i]!=s2[j+1])  j = Next[j];\n            if (s1[i]==s2[j+1])  j++;\n            dp[i] = dp[i-1];\n            if (j==len2){\n                dp[i]++;\n                j = Next[j];\n            }\n        }\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 tmp = q.front();    q.pop();\n            if (Len[tmp]&amp;1)                     \/\/\u62d3\u6251\u6392\u5e8f\u8fdb\u884c\u65f6\uff0c\u7edf\u8ba1\u7b54\u6848\u3002\n                ans = (ans+GetAns(mark[tmp], Len[tmp])*times[tmp])%mod;\n            times[fail[tmp]] += times[tmp];\n            in[fail[tmp]]--;\n            if (in[fail[tmp]]==0)\n                q.push(fail[tmp]);\n        }\n        return;\n    }\n    ll GetAns(int idx, int len){\n        int l = idx-len+1 + len2-1-1;\n        if (l&gt;idx)  return 0;\n        return dp[idx]-dp[l];\n    }\n}PAM;\nPAM P;\nint main(){\n    scanf (&quot;%d %d&quot;,&amp;P.len1,&amp;P.len2);\n    scanf (&quot;%s %s&quot;,P.s1+1,P.s2+1);\n    P.KMP();\n    P.Build();\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6d1b\u8c37 P4555 \u6700\u957f\u53cc\u56de\u6587\u4e32 \u8f93\u5165\u957f\u5ea6\u4e3an\u7684\u4e32S\uff0c\u6c42S\u7684\u6700\u957f\u53cc\u56de\u6587\u5b50\u4e32T,\u5373\u53ef\u5c06T\u5206\u4e3a\u4e24\u90e8\u5206X\uff0cY [&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\/385"}],"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=385"}],"version-history":[{"count":7,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"predecessor-version":[{"id":552,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/385\/revisions\/552"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}