最長回文子串 php

2021-09-28 15:19:06 字數 1065 閱讀 6106

<?php

/**************** 暴力解法 ****************/

class

solution}}

return

$max;}

}/**************** 動態規劃 ****************/

class

solution1}}

return

substr($s

,$left

,$right

-$left+1

);//擷取字串}}

/**************** 中心擴充套件法 ****************/

class

solution2

}return

substr($s

,$left

,$right

-$left+1

);//擷取字串

}private

function

centerexpand

($left

,$right

)//當不滿足條件是,左右都再進了一位,此時不是常規的$right-$left+1,而是要-1

return

$right

-$left-1

;}}/**************** 馬拉車演算法 ****************/

class

solution3

while

($str[$i

-$r[$i

]-1]

==$str[$i

+$r[$i

]+1]

)if($i

+$r[$i

]>

$maxright)if

(1+2

*$r[$i

]>

strlen

($maxstr))

}return

str_replace

('#',''

,$maxstr);

}}?>

php 最長回文子串

最長回文子串 由於case包含奇偶性,所以分兩種情況討論 思路 找到以字元 x 為中心的最長回文子串 從x的下標開始遍歷,拆分為偶數對稱情況和奇數對稱情況 終止條件有2 對稱位置的字元不相同 迴圈右側下標超出字元長度 結果 1364 ms 14.9 mb 主要難點是 計算下標 class solut...

最長回文子串 最長回文子串行

1.最長回文子串行 可以不連續 include include include include using namespace std 遞迴方法,求解最長回文子串行 intlps char str,int i,int j intmain include include include using n...

最長回文子串

描述 輸入乙個字串,求出其中最長的回文子串。子串的含義是 在原串連續出現的字串片段。回文的含義是 正著看和倒著看是相同的,如abba和abbebba。在判斷是要求忽略所有的標點和空格,且忽略大小寫,但輸出時按原樣輸出 首尾不要輸出多餘的字串 輸入字串長度大於等於1小於等於5000,且單獨佔一行 如果...