{"id":114110,"date":"2026-10-08T11:09:04","date_gmt":"2026-10-08T03:09:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/114110.html"},"modified":"2026-10-08T11:09:04","modified_gmt":"2026-10-08T03:09:04","slug":"%e3%80%90%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92-2%e3%80%915-%e6%9c%80%e9%95%bf%e5%9b%9e%e6%96%87%e5%ad%90%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/114110.html","title":{"rendered":"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32"},"content":{"rendered":"<h2>\u9898\u76ee\u63cf\u8ff0&#xff1a;<\/h2>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s&#xff0c;\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32\u3002<\/p>\n<p>\u5982\u679c\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u90fd\u76f8\u540c&#xff0c;\u5219\u5b83\u6ee1\u8db3 \u56de\u6587\u6027\u3002<\/p>\n<p>\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684 \u975e\u7a7a \u5b57\u7b26\u5e8f\u5217\u3002<\/p>\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b 1&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a;s &#061; &#034;babad&#034;<br \/>\n\u8f93\u51fa&#xff1a;&#034;bab&#034;<br \/>\n\u89e3\u91ca&#xff1a;&#034;aba&#034; \u540c\u6837\u662f\u7b26\u5408\u9898\u610f\u7684\u7b54\u6848\u3002<\/p>\n<p>\u793a\u4f8b 2&#xff1a;<\/p>\n<p>\u8f93\u5165&#xff1a;s &#061; &#034;cbbd&#034;<br \/>\n\u8f93\u51fa&#xff1a;&#034;bb&#034;<\/p>\n<h2><span style=\"color:#fe2c24\">\u89e3\u9898\u601d\u8def<\/span>&#xff1a;<\/h2>\n<h3>\u65b9\u6cd5\u4e00&#xff1a;\u4e2d\u5fc3\u6269\u5c55\u6cd5(\u6700\u4f18\u89e3)<\/h3>\n<h4>\u6838\u5fc3\u601d\u8def&#xff1a;<\/h4>\n<p>\u56de\u6587\u4e32\u4e00\u5b9a\u6709\u4e00\u4e2a\u4e2d\u5fc3&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5947\u6570\u957f\u5ea6&#xff1a;\u4e2d\u5fc3\u662f\u4e00\u4e2a\u5b57\u7b26&#xff08;\u5982 &#034;aba&#034; \u4e2d\u5fc3\u662f b&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u5076\u6570\u957f\u5ea6&#xff1a;\u4e2d\u5fc3\u662f\u4e24\u4e2a\u5b57\u7b26\u4e4b\u95f4&#xff08;\u5982 &#034;abba&#034; \u4e2d\u5fc3\u662f bb \u4e4b\u95f4&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u4ece\u6bcf\u4e2a\u4e2d\u5fc3\u5411\u4e24\u8fb9\u6269\u5c55&#xff0c;\u627e\u5230\u6700\u957f\u7684\u56de\u6587\u3002<\/p>\n<h4>\u5177\u4f53\u8fc7\u7a0b\u793a\u4f8b&#xff1a;<\/h4>\n<p>s &#061; &#034;babad&#034;<\/p>\n<p>\u4e2d\u5fc3 i&#061;0 (&#039;b&#039;): \u6269\u5c55 \u2192 &#034;b&#034;<br \/>\n\u4e2d\u5fc3 i&#061;1 (&#039;a&#039;): \u6269\u5c55 \u2192 &#034;bab&#034;<br \/>\n\u4e2d\u5fc3 i&#061;2 (&#039;b&#039;): \u6269\u5c55 \u2192 &#034;aba&#034;<br \/>\n\u4e2d\u5fc3 i&#061;3 (&#039;a&#039;): \u6269\u5c55 \u2192 &#034;a&#034;<br \/>\n\u4e2d\u5fc3 i&#061;4 (&#039;d&#039;): \u6269\u5c55 \u2192 &#034;d&#034;<\/p>\n<p>\u5076\u6570\u4e2d\u5fc3:<br \/>\n\u4e2d\u5fc3 i&#061;0.5: \u6269\u5c55 \u2192 &#034;&#034;<br \/>\n\u4e2d\u5fc3 i&#061;1.5: \u6269\u5c55 \u2192 &#034;&#034;<br \/>\n&#8230;<\/p>\n<p>\u6700\u957f: &#034;bab&#034; \u6216 &#034;aba&#034; \u2705<\/p>\n<h4>\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h4>\n<p>class Solution {<br \/>\npublic:<br \/>\n    string longestPalindrome(string s) {<br \/>\n        if (s.empty()) return &#034;&#034;;<\/p>\n<p>        int start &#061; 0, maxLen &#061; 1;<\/p>\n<p>        for (int i &#061; 0; i &lt; s.size(); i&#043;&#043;) {<br \/>\n            \/\/ \u5947\u6570\u957f\u5ea6\u56de\u6587&#xff0c;\u4e2d\u5fc3\u662f s[i]<br \/>\n            int len1 &#061; expandAroundCenter(s, i, i);<br \/>\n            \/\/ \u5076\u6570\u957f\u5ea6\u56de\u6587&#xff0c;\u4e2d\u5fc3\u662f s[i] \u548c s[i&#043;1] \u4e4b\u95f4<br \/>\n            int len2 &#061; expandAroundCenter(s, i, i &#043; 1);<\/p>\n<p>            int len &#061; max(len1, len2);<br \/>\n            if (len &gt; maxLen) {<br \/>\n                maxLen &#061; len;<br \/>\n                start &#061; i &#8211; (len &#8211; 1) \/ 2;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return s.substr(start, maxLen);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    int expandAroundCenter(string&amp; s, int left, int right) {<br \/>\n        while (left &gt;&#061; 0 &amp;&amp; right &lt; s.size() &amp;&amp; s[left] &#061;&#061; s[right]) {<br \/>\n            left&#8211;;<br \/>\n            right&#043;&#043;;<br \/>\n        }<br \/>\n        return right &#8211; left &#8211; 1;  \/\/ \u56de\u6587\u957f\u5ea6<br \/>\n    }<br \/>\n};<\/p>\n<h4>\u590d\u6742\u5ea6\u5206\u6790<\/h4>\n<table>\n<tr>\u7ef4\u5ea6\u590d\u6742\u5ea6\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(n\u00b2)<\/td>\n<td>\u6bcf\u4e2a\u4e2d\u5fc3\u6269\u5c55 O(n)&#xff0c;\u5171 n \u4e2a\u4e2d\u5fc3<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(1)<\/td>\n<td>\u53ea\u7528\u5e38\u6570\u4e2a\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u65b9\u6cd5\u4e8c&#xff1a;\u52a8\u6001\u89c4\u5212<\/h3>\n<h4>\u601d\u8def&#xff1a;<\/h4>\n<p>dp[i][j] \u8868\u793a s[i..j] \u662f\u5426\u662f\u56de\u6587\u3002<\/p>\n<ul>\n<li>\n<p>s[i] &#061;&#061; s[j] \u4e14 dp[i&#043;1][j-1] \u4e3a\u771f \u2192 dp[i][j] \u4e3a\u771f<\/p>\n<\/li>\n<li>\n<p>\u8fb9\u754c&#xff1a;j &#8211; i &lt;&#061; 1 \u65f6&#xff0c;\u53ea\u8981 s[i] &#061;&#061; s[j] \u5c31\u662f\u56de\u6587<\/p>\n<\/li>\n<\/ul>\n<h4>\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h4>\n<p>class Solution {<br \/>\npublic:<br \/>\n    string longestPalindrome(string s) {<br \/>\n        int n &#061; s.size();<br \/>\n        if (n &lt; 2) return s;<\/p>\n<p>        vector&lt;vector&lt;bool&gt;&gt; dp(n, vector&lt;bool&gt;(n, false));<br \/>\n        int start &#061; 0, maxLen &#061; 1;<\/p>\n<p>        \/\/ \u521d\u59cb\u5316&#xff1a;\u5355\u4e2a\u5b57\u7b26\u90fd\u662f\u56de\u6587<br \/>\n        for (int i &#061; 0; i &lt; n; i&#043;&#043;) {<br \/>\n            dp[i][i] &#061; true;<br \/>\n        }<\/p>\n<p>        \/\/ \u6309\u957f\u5ea6\u9012\u589e\u904d\u5386<br \/>\n        for (int len &#061; 2; len &lt;&#061; n; len&#043;&#043;) {<br \/>\n            for (int i &#061; 0; i &#043; len &#8211; 1 &lt; n; i&#043;&#043;) {<br \/>\n                int j &#061; i &#043; len &#8211; 1;<\/p>\n<p>                if (s[i] &#061;&#061; s[j]) {<br \/>\n                    if (len &#061;&#061; 2 || dp[i&#043;1][j-1]) {<br \/>\n                        dp[i][j] &#061; true;<br \/>\n                        if (len &gt; maxLen) {<br \/>\n                            maxLen &#061; len;<br \/>\n                            start &#061; i;<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        return s.substr(start, maxLen);<br \/>\n    }<br \/>\n};<\/p>\n<p>\u590d\u6742\u5ea6&#xff1a;\u65f6\u95f4 O(n\u00b2)&#xff0c;\u7a7a\u95f4 O(n\u00b2)<\/p>\n<h3>\u65b9\u6cd5\u4e09&#xff1a;Manacher \u7b97\u6cd5<\/h3>\n<h4>\u6838\u5fc3\u601d\u8def&#xff1a;<\/h4>\n<p>\u7b2c\u4e00\u6b65&#xff1a;\u9884\u5904\u7406&#xff0c;\u7edf\u4e00\u5947\u5076<\/p>\n<p>\u56de\u6587\u6709\u4e24\u79cd&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5947\u6570\u957f\u5ea6&#xff1a;&#034;aba&#034;&#xff0c;\u4e2d\u5fc3\u662f\u5355\u4e2a\u5b57\u7b26<\/p>\n<\/li>\n<li>\n<p>\u5076\u6570\u957f\u5ea6&#xff1a;&#034;abba&#034;&#xff0c;\u4e2d\u5fc3\u662f\u4e24\u4e2a\u5b57\u7b26\u4e4b\u95f4<\/p>\n<\/li>\n<\/ul>\n<p>Manacher \u7684\u505a\u6cd5\u662f\u63d2\u5165\u7279\u6b8a\u5b57\u7b26&#xff0c;\u628a\u6240\u6709\u56de\u6587\u90fd\u53d8\u6210\u5947\u6570\u957f\u5ea6\u3002<\/p>\n<p>\u63d2\u5165 #&#xff1a;<\/p>\n<p>\u539f\u4e32:  a  b  a<br \/>\n\u65b0\u4e32:  #  a  #  b  #  a  #<\/p>\n<p>\u539f\u4e32:  a  b  b  a<br \/>\n\u65b0\u4e32:  #  a  #  b  #  b  #  a  #<\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u539f\u6765\u5947\u6570\u957f\u5ea6 &#034;aba&#034;&#xff08;\u957f\u5ea63&#xff09;\u2192 \u65b0\u4e32 &#034;#a#b#a#&#034;&#xff08;\u957f\u5ea67&#xff09;&#xff0c;\u4e2d\u5fc3\u662f b<\/p>\n<\/li>\n<li>\n<p>\u539f\u6765\u5076\u6570\u957f\u5ea6 &#034;abba&#034;&#xff08;\u957f\u5ea64&#xff09;\u2192 \u65b0\u4e32 &#034;#a#b#b#a#&#034;&#xff08;\u957f\u5ea69&#xff09;&#xff0c;\u4e2d\u5fc3\u662f #<\/p>\n<\/li>\n<\/ul>\n<p>\u6240\u6709\u56de\u6587\u90fd\u53d8\u6210\u5947\u6570\u957f\u5ea6&#xff0c;\u4e2d\u5fc3\u552f\u4e00\u3002<\/p>\n<p>\u518d\u52a0\u4e24\u4e2a\u54e8\u5175&#xff1a;<\/p>\n<p>\u65b0\u4e32:  ^  #  a  #  b  #  a  #  $<\/p>\n<ul>\n<li>\n<p>^ \u548c $ \u662f\u54e8\u5175&#xff0c;\u9632\u6b62\u6269\u5c55\u65f6\u8d8a\u754c<\/p>\n<\/li>\n<li>\n<p>\u5b83\u4eec\u4e0d\u76f8\u7b49&#xff0c;\u6269\u5c55\u5230\u8fd9\u91cc\u4e00\u5b9a\u505c\u6b62<\/p>\n<\/li>\n<\/ul>\n<p>\u7b2c\u4e8c\u6b65&#xff1a;\u5b9a\u4e49\u534a\u5f84\u6570\u7ec4 p<\/p>\n<p>p[i] \u8868\u793a\u4ee5 i \u4e3a\u4e2d\u5fc3\u7684\u56de\u6587\u534a\u5f84&#xff08;\u5305\u542b\u4e2d\u5fc3&#xff09;\u3002<\/p>\n<p>\u4ee5 &#034;#a#b#a#&#034; \u4e3a\u4f8b&#xff1a;<\/p>\n<table>\n<tr>i\u5b57\u7b26p[i]\u56de\u6587<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>#<\/td>\n<td>0<\/td>\n<td>#<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>a<\/td>\n<td>1<\/td>\n<td>#a#<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>#<\/td>\n<td>0<\/td>\n<td>#<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>b<\/td>\n<td>3<\/td>\n<td>#a#b#a#<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>#<\/td>\n<td>0<\/td>\n<td>#<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>a<\/td>\n<td>1<\/td>\n<td>#a#<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>#<\/td>\n<td>0<\/td>\n<td>#<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u539f\u4e32\u56de\u6587\u957f\u5ea6 &#061; p[i]&#xff08;\u56e0\u4e3a\u63d2\u5165 # \u540e&#xff0c;\u534a\u5f84\u6b63\u597d\u7b49\u4e8e\u539f\u4e32\u56de\u6587\u957f\u5ea6&#xff09;\u3002<\/p>\n<p>\u539f\u4e32\u8d77\u59cb\u4f4d\u7f6e &#061; (i &#8211; p[i]) \/ 2\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff1a;\u6838\u5fc3\u2014\u2014\u5229\u7528\u5bf9\u79f0\u6027<\/p>\n<p>\u5173\u952e\u53d8\u91cf&#xff1a;<\/p>\n<ul>\n<li>\n<p>center&#xff1a;\u5f53\u524d\u6700\u53f3\u56de\u6587\u7684\u4e2d\u5fc3<\/p>\n<\/li>\n<li>\n<p>right&#xff1a;\u5f53\u524d\u6700\u53f3\u56de\u6587\u7684\u53f3\u8fb9\u754c&#xff08;center &#043; p[center]&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;<\/p>\n<p>\u5f53\u904d\u5386\u5230 i \u65f6&#xff0c;\u5982\u679c i &lt; right&#xff0c;\u8bf4\u660e i \u5728\u67d0\u4e2a\u56de\u6587\u5185\u90e8\u3002<\/p>\n<p>\u5229\u7528\u5bf9\u79f0\u6027&#xff0c;i \u5173\u4e8e center \u7684\u5bf9\u79f0\u70b9\u662f&#xff1a;<\/p>\n<p>mirror &#061; 2 * center &#8211; i<\/p>\n<p>\u6b64\u65f6 p[i] \u81f3\u5c11\u7b49\u4e8e p[mirror]&#xff0c;\u4f46\u6709\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<p>\u60c5\u51b51: p[mirror] &lt; right &#8211; i<br \/>\n       \u2192 p[i] &#061; p[mirror]&#xff08;\u5b8c\u5168\u5bf9\u79f0&#xff09;<\/p>\n<p>\u60c5\u51b52: p[mirror] &gt;&#061; right &#8211; i<br \/>\n       \u2192 p[i] &#061; right &#8211; i&#xff08;\u53ea\u80fd\u786e\u5b9a\u8fd9\u4e48\u591a&#xff0c;\u9700\u8981\u7ee7\u7eed\u6269\u5c55&#xff09;<\/p>\n<p>\u7edf\u4e00\u5199\u6cd5&#xff1a;<\/p>\n<p>if (i &lt; right) {<br \/>\n    p[i] &#061; min(right &#8211; i, p[mirror]);<br \/>\n}<\/p>\n<p>\u56fe\u89e3&#xff1a;<\/p>\n<p>      center<br \/>\n        \u2193<br \/>\n  &#8230; [ &#8230; i &#8230; ] &#8230;<br \/>\n        \u2191         \u2191<br \/>\n      mirror    right<\/p>\n<p>i \u548c mirror \u5173\u4e8e center \u5bf9\u79f0<\/p>\n<p>\u7b2c\u56db\u6b65&#xff1a;\u7ee7\u7eed\u6269\u5c55<\/p>\n<p>\u786e\u5b9a p[i] \u7684\u4e0b\u754c\u540e&#xff0c;\u7ee7\u7eed\u5411\u4e24\u8fb9\u6269\u5c55&#xff1a;<\/p>\n<p>while (t[i &#043; p[i] &#043; 1] &#061;&#061; t[i &#8211; p[i] &#8211; 1]) {<br \/>\n    p[i]&#043;&#043;;<br \/>\n}<\/p>\n<p>\u56e0\u4e3a\u52a0\u4e86\u54e8\u5175 ^ \u548c $&#xff0c;\u4e0d\u4f1a\u8d8a\u754c\u3002<\/p>\n<p>\u7b2c\u4e94\u6b65&#xff1a;\u66f4\u65b0 center \u548c right<\/p>\n<p>\u5982\u679c i &#043; p[i] &gt; right&#xff0c;\u8bf4\u660e\u627e\u5230\u4e86\u66f4\u9760\u53f3\u7684\u56de\u6587&#xff0c;\u66f4\u65b0&#xff1a;<\/p>\n<p>if (i &#043; p[i] &gt; right) {<br \/>\n    center &#061; i;<br \/>\n    right &#061; i &#043; p[i];<br \/>\n}<\/p>\n<h4>\u7528\u4f8b\u5b50\u8d70\u4e00\u904d&#xff1a;<\/h4>\n<p>s &#061; &#034;babad&#034;<\/p>\n<p>\u9884\u5904\u7406&#xff1a;<\/p>\n<p>t &#061; &#034;^#b#a#b#a#d#$&#034;<br \/>\n\u4e0b\u6807: 0 1 2 3 4 5 6 7 8 9 10 11 12<\/p>\n<p>\u904d\u5386&#xff1a;<\/p>\n<table>\n<tr>it[i]mirrorrightp[i]\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>#<\/td>\n<td>&#8211;<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>\u6269\u5c55\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>b<\/td>\n<td>&#8211;<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>#b#<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>#<\/td>\n<td>&#8211;<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>\u6269\u5c55\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>a<\/td>\n<td>&#8211;<\/td>\n<td>0<\/td>\n<td>3<\/td>\n<td>#b#a#b#&#xff0c;center&#061;4, right&#061;7<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>#<\/td>\n<td>3<\/td>\n<td>7<\/td>\n<td>0<\/td>\n<td>i&lt;right&#xff0c;p[5]&#061;min(2, p[3]&#061;0)&#061;0<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>b<\/td>\n<td>2<\/td>\n<td>7<\/td>\n<td>1<\/td>\n<td>i&lt;right&#xff0c;p[6]&#061;min(1, p[2]&#061;1)&#061;1<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>#<\/td>\n<td>1<\/td>\n<td>7<\/td>\n<td>0<\/td>\n<td>i&#061;right&#xff0c;\u6269\u5c55\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>a<\/td>\n<td>&#8211;<\/td>\n<td>7<\/td>\n<td>1<\/td>\n<td>i&gt;right&#xff0c;\u6269\u5c55 #a#<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>#<\/td>\n<td>&#8211;<\/td>\n<td>7<\/td>\n<td>0<\/td>\n<td>\u00a0<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>d<\/td>\n<td>&#8211;<\/td>\n<td>7<\/td>\n<td>1<\/td>\n<td>#d#<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>maxLen &#061; 3, maxCenter &#061; 4<\/p>\n<p>start &#061; (4 &#8211; 3) \/ 2 &#061; 0<br \/>\n\u7ed3\u679c &#061; s.substr(0, 3) &#061; &#034;bab&#034; \u2705<\/p>\n<h4>\u4ee3\u7801\u5b9e\u73b0&#xff1a;<\/h4>\n<p>class Solution {<br \/>\npublic:<br \/>\n    string longestPalindrome(string s) {<br \/>\n        \/\/ \u9884\u5904\u7406&#xff1a;\u63d2\u5165 # \u53d8\u6210\u5947\u6570\u957f\u5ea6<br \/>\n        string t &#061; &#034;^#&#034;;<br \/>\n        for (char c : s) {<br \/>\n            t &#043;&#061; c;<br \/>\n            t &#043;&#061; &#039;#&#039;;<br \/>\n        }<br \/>\n        t &#043;&#061; &#039;$&#039;;<\/p>\n<p>        int n &#061; t.size();<br \/>\n        vector&lt;int&gt; p(n, 0);<br \/>\n        int center &#061; 0, right &#061; 0;<br \/>\n        int maxLen &#061; 0, maxCenter &#061; 0;<\/p>\n<p>        for (int i &#061; 1; i &lt; n &#8211; 1; i&#043;&#043;) {<br \/>\n            if (i &lt; right) {<br \/>\n                p[i] &#061; min(right &#8211; i, p[2 * center &#8211; i]);<br \/>\n            }<\/p>\n<p>            while (t[i &#043; p[i] &#043; 1] &#061;&#061; t[i &#8211; p[i] &#8211; 1]) {<br \/>\n                p[i]&#043;&#043;;<br \/>\n            }<\/p>\n<p>            if (i &#043; p[i] &gt; right) {<br \/>\n                center &#061; i;<br \/>\n                right &#061; i &#043; p[i];<br \/>\n            }<\/p>\n<p>            if (p[i] &gt; maxLen) {<br \/>\n                maxLen &#061; p[i];<br \/>\n                maxCenter &#061; i;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        int start &#061; (maxCenter &#8211; maxLen) \/ 2;<br \/>\n        return s.substr(start, maxLen);<br \/>\n    }<br \/>\n};<\/p>\n<h4>\u590d\u6742\u5ea6\u5206\u6790&#xff1a;<\/h4>\n<table>\n<tr>\u7ef4\u5ea6\u590d\u6742\u5ea6\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(n)<\/td>\n<td>\u6bcf\u4e2a\u5b57\u7b26\u6700\u591a\u88ab\u6269\u5c55\u4e00\u6b21<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(n)<\/td>\n<td>p \u6570\u7ec4 &#043; \u9884\u5904\u7406\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3a\u4ec0\u4e48\u662f O(n)&#xff1f;<br \/>\n\u56e0\u4e3a right \u53ea\u589e\u4e0d\u51cf&#xff0c;\u6bcf\u6b21\u6269\u5c55\u90fd\u4f1a\u589e\u52a0 right&#xff0c;\u603b\u6269\u5c55\u6b21\u6570\u4e0d\u8d85\u8fc7 n\u3002<\/p>\n<h3>\u4e09\u79cd\u65b9\u6cd5\u5bf9\u6bd4&#xff1a;<\/h3>\n<table>\n<tr>\u65b9\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u7a7a\u95f4\u590d\u6742\u5ea6\u4ee3\u7801\u590d\u6742\u5ea6\u63a8\u8350\u5ea6<\/tr>\n<tbody>\n<tr>\n<td>\u4e2d\u5fc3\u6269\u5c55<\/td>\n<td>O(n\u00b2)<\/td>\n<td>O(1)<\/td>\n<td>\u7b80\u5355<\/td>\n<td>\u2b50\u2b50\u2b50\u2b50\u2b50<\/td>\n<\/tr>\n<tr>\n<td>\u52a8\u6001\u89c4\u5212<\/td>\n<td>O(n\u00b2)<\/td>\n<td>O(n\u00b2)<\/td>\n<td>\u4e2d\u7b49<\/td>\n<td>\u2b50\u2b50\u2b50\u2b50<\/td>\n<\/tr>\n<tr>\n<td>Manacher<\/td>\n<td>O(n)<\/td>\n<td>O(n)<\/td>\n<td>\u590d\u6742<\/td>\n<td>\u2b50\u2b50\u2b50<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e2d\u5fc3\u6269\u5c55\u662f\u9762\u8bd5\u9996\u9009&#xff1a;\u4ee3\u7801\u7b80\u6d01&#xff0c;\u7a7a\u95f4 O(1)&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6 O(n\u00b2) \u5bf9\u5927\u591a\u6570\u573a\u666f\u591f\u7528\u3002<\/p>\n<h4>\u5173\u952e\u7ec6\u8282&#xff1a;<\/h4>\n<p>1. \u4e3a\u4ec0\u4e48\u4e2d\u5fc3\u6269\u5c55\u8981\u5904\u7406\u5947\u5076\u4e24\u79cd\u60c5\u51b5&#xff1f;<\/p>\n<ul>\n<li>\n<p>\u5947\u6570\u56de\u6587&#xff1a;&#034;aba&#034;&#xff0c;\u4e2d\u5fc3\u662f\u5355\u4e2a\u5b57\u7b26<\/p>\n<\/li>\n<li>\n<p>\u5076\u6570\u56de\u6587&#xff1a;&#034;abba&#034;&#xff0c;\u4e2d\u5fc3\u662f\u4e24\u4e2a\u5b57\u7b26\u4e4b\u95f4<\/p>\n<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u9700\u8981\u5bf9\u6bcf\u4e2a\u4f4d\u7f6e\u8c03\u7528\u4e24\u6b21\u6269\u5c55&#xff1a;expand(i, i) \u548c expand(i, i&#043;1)\u3002<\/p>\n<p>2. \u4e3a\u4ec0\u4e48\u8fd4\u56de right &#8211; left &#8211; 1&#xff1f;<\/p>\n<p>\u5faa\u73af\u7ed3\u675f\u65f6&#xff0c;left \u548c right \u5df2\u7ecf\u8d8a\u754c\u6216\u4e0d\u5339\u914d\u3002\u56de\u6587\u957f\u5ea6 &#061; (right &#8211; 1) &#8211; (left &#043; 1) &#043; 1 &#061; right &#8211; left &#8211; 1\u3002<\/p>\n<p>3. \u52a8\u6001\u89c4\u5212\u7684\u904d\u5386\u987a\u5e8f<\/p>\n<p>\u5fc5\u987b\u6309\u957f\u5ea6\u9012\u589e\u904d\u5386&#xff0c;\u56e0\u4e3a dp[i][j] \u4f9d\u8d56 dp[i&#043;1][j-1]&#xff08;\u66f4\u77ed\u7684\u5b50\u4e32&#xff09;\u3002<\/p>\n<h2>\u603b\u7ed3&#xff1a;<\/h2>\n<table>\n<tr>\u8981\u70b9\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u6838\u5fc3\u601d\u60f3<\/td>\n<td>\u4ece\u6bcf\u4e2a\u4e2d\u5fc3\u5411\u4e24\u8fb9\u6269\u5c55<\/td>\n<\/tr>\n<tr>\n<td>\u5173\u952e\u64cd\u4f5c<\/td>\n<td>\u5947\u6570\u4e2d\u5fc3 (i,i)&#xff0c;\u5076\u6570\u4e2d\u5fc3 (i,i&#043;1)<\/td>\n<\/tr>\n<tr>\n<td>\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(n\u00b2)<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td>\n<td>O(1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0&#xff1a;\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s&#xff0c;\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u90fd\u76f8\u540c&#xff0c;\u5219\u5b83\u6ee1\u8db3 \u56de\u6587\u6027\u3002\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684 \u975e\u7a7a \u5b57\u7b26\u5e8f\u5217\u3002\u793a\u4f8b 1&#xff1a;\u8f93\u5165&#xff1a;s  \\&#8221;babad\\&#8221;<br \/>\n\u8f93\u51fa&#xff1a;\\&#8221;bab\\&#8221;<br \/>\n\u89e3\u91ca&#xff1a;\\&#8221;aba\\&#8221; \u540c\u6837\u662f\u7b26\u5408\u9898\u610f\u7684\u7b54\u6848\u3002\u793a\u4f8b 2&#xff1a;\u8f93\u5165&#xff1a;s  \\&#8221;cbbd\\&#8221;<br \/>\n\u8f93\u51fa&#xff1a;\\&#8221;bb\\&#8221;\u89e3\u9898\u601d\u8def&#xff1a;\u65b9\u6cd5\u4e00&amp;#xff1a<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,2692,2865,1813,427],"topic":[],"class_list":["post-114110","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-leetcode","tag-2865","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/114110.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9898\u76ee\u63cf\u8ff0&#xff1a;\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s&#xff0c;\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u90fd\u76f8\u540c&#xff0c;\u5219\u5b83\u6ee1\u8db3 \u56de\u6587\u6027\u3002\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684 \u975e\u7a7a \u5b57\u7b26\u5e8f\u5217\u3002\u793a\u4f8b 1&#xff1a;\u8f93\u5165&#xff1a;s &quot;babad&quot; \u8f93\u51fa&#xff1a;&quot;bab&quot; \u89e3\u91ca&#xff1a;&quot;aba&quot; \u540c\u6837\u662f\u7b26\u5408\u9898\u610f\u7684\u7b54\u6848\u3002\u793a\u4f8b 2&#xff1a;\u8f93\u5165&#xff1a;s &quot;cbbd&quot; \u8f93\u51fa&#xff1a;&quot;bb&quot;\u89e3\u9898\u601d\u8def&#xff1a;\u65b9\u6cd5\u4e00&amp;#xff1a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/114110.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-08T03:09:04+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/114110.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/114110.html\",\"name\":\"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-08T03:09:04+00:00\",\"dateModified\":\"2026-10-08T03:09:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/114110.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/114110.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/114110.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/114110.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9898\u76ee\u63cf\u8ff0&#xff1a;\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 s&#xff0c;\u627e\u5230 s \u4e2d\u6700\u957f\u7684\u56de\u6587\u5b50\u4e32\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5411\u524d\u548c\u5411\u540e\u8bfb\u90fd\u76f8\u540c&#xff0c;\u5219\u5b83\u6ee1\u8db3 \u56de\u6587\u6027\u3002\u5b50\u5b57\u7b26\u4e32 \u662f\u5b57\u7b26\u4e32\u4e2d\u8fde\u7eed\u7684 \u975e\u7a7a \u5b57\u7b26\u5e8f\u5217\u3002\u793a\u4f8b 1&#xff1a;\u8f93\u5165&#xff1a;s \"babad\" \u8f93\u51fa&#xff1a;\"bab\" \u89e3\u91ca&#xff1a;\"aba\" \u540c\u6837\u662f\u7b26\u5408\u9898\u610f\u7684\u7b54\u6848\u3002\u793a\u4f8b 2&#xff1a;\u8f93\u5165&#xff1a;s \"cbbd\" \u8f93\u51fa&#xff1a;\"bb\"\u89e3\u9898\u601d\u8def&#xff1a;\u65b9\u6cd5\u4e00&amp;#xff1a","og_url":"https:\/\/www.wsisp.com\/helps\/114110.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-08T03:09:04+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/114110.html","url":"https:\/\/www.wsisp.com\/helps\/114110.html","name":"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-08T03:09:04+00:00","dateModified":"2026-10-08T03:09:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/114110.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/114110.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/114110.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u52a8\u6001\u89c4\u5212-2\u30115.\u6700\u957f\u56de\u6587\u5b50\u4e32"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/114110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=114110"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/114110\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=114110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=114110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=114110"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=114110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}