最長回文子串 V2(Manacher演算法)

2021-08-01 20:15:31 字數 722 閱讀 2318

回文串是指aba、abba、cccbccc、aaaa這種左右對稱的字串。

輸入乙個字串str,輸出str裡最長回文子串的長度。

input

輸入str(str的長度 <= 100000)
output

輸出最長回文子串的長度l。
input示例

daabaac
output示例

5

#include #include #include #include using namespace std;

int fun(string &str)

temp += '#';

temp += '&';

int size = len*2 + 1;

int pos = 0;

int right = 0;

int result = 0;

int buf[size];

for (int i = 1; i < size; i++)

else

while (temp[i-buf[i]] == temp[i+buf[i]])

if (i+buf[i] > right)

result = max(result, buf[i]);

} return result-1;

}int main()

高效的求最長回文子串的演算法 Manacher

gdoi市選出了一道關於manacher的題目,於是打算學一下,但因為網路流鴿了一陣,最近才學完,學完後表示複雜度有點迷。manacher,俗稱馬拉車,是由一位名叫manacher的人與1975年提出的,這個演算法讓求最長回文子串的複雜度從o n 2 下降到了o n 先從n方演算法說起,n方演算法是...

最長回文子串 V2(Manacher演算法)

回文串是指aba abba cccbccc aaaa這種左右對稱的字串。輸入乙個字串str,輸出str裡最長回文子串的長度。input 輸入str str的長度 100000 output 輸出最長回文子串的長度l。sample input daabaacsample output 5 includ...

最長回文子串 2

忽略字串中的大小寫和標點 include include include define maxn 5000 10 測試資料 confuciuss say madam,i m adam 忽略大小寫 char buf maxn 儲存源字串 char s maxn 儲存去除標點並改為大寫後的字串,方便判斷...