題解:首先要區分回文子串 和 回文子串行的區別。本題是求最長回文子串。其次需要考慮給所求得的回文子串總長度是奇數還是偶數,如果是奇數則是aba形式,如果是偶數則是abba形式。最後就是求解,這個演算法題太普遍了,只需要給字串增加乙個輔助字元#,即可全部當做奇數字串來處理了。
python版本:
str2 = #儲存輔助字元陣列,輔助字串用井號間隔
for s in str:
str2.pop(-1)
str3 = "".join(str2) #str3輔助字串
def findmax(s,i): #在輔助字串中尋找最長回文子串的半徑
# print(s)
c = 0
tag = 1
while i - c >= 0 and i + c retmax:
retindex,retmax = j,ret[j]
print(getret(str3,retindex,retmax)) #列印結果
最長回文串 演算法 4 求解最長回文子串
問題描述 給定乙個字串 s,找到 s 中最長的回文子串。你可以假設 s 的最大長度為 1000。注釋 回文通俗的說就是正著念和反著念都一樣,如 上海自來水來自海上。示例 1 輸入 dabccbac 輸出 abccba 示例 2 輸入 cbbd 輸出 bb 解題思路 此處撰寫解題思路 遍歷每乙個索引,...
最長回文子串 最長回文子串行
1.最長回文子串行 可以不連續 include include include include using namespace std 遞迴方法,求解最長回文子串行 intlps char str,int i,int j intmain include include include using n...
day4 最長回文子串
給定乙個字串 s,找到 s 中最長的回文子串。你可以假設 s 的最大長度為 1000。示例 1 輸入 babad 輸出 bab 注意 aba 也是乙個有效答案。示例 2 輸入 cbbd 輸出 bb 這道題比較煩人的是判斷回文子串。因此需要一種能夠快速判斷原字串的所有子串是否是回文子串的方法,於是想到...