最长回文子串---Manacher算法
2016-09-03 13:21
281 查看
百度:Manacher算法
代码
#include <iostream> #include <string> #include <cstring> #include <algorithm> using namespace std; int MANACHER(const string &str) { string data; int len = str.length(); data+="$#"; for (int i=0;i<len;i++) { data+=str[i]; data+='#'; } data+='@'; //到此预处理完成 len = data.length(); int *temp = new int[len+2]; memset(temp,0,sizeof(temp)); int MaxIndex=0; int i=1,index=0,res=0; for(;i<len;i++) { if (MaxIndex>i) { temp[i]=(MaxIndex-i<temp[2*index-i]?MaxIndex-i:temp[2*index-i]); } else temp[i]=1; while (data[i-temp[i]] == data[i+temp[i]]) { temp[i]++; } if(i+temp[i] > MaxIndex) { MaxIndex = i+temp[i]; index=i; } res = (res>temp[i]?res:temp[i]); } delete[]temp; return res-1; } int main() { string str ; int n; while(cin >> n) { while (n--) { cin >> str; cout << MANACHER(str)<<endl; } } return 0; }
相关文章推荐
- manacher求最长回文子串算法模板
- Manacher 算法讲解 O(N)复杂度的 最长回文子串求解
- 最长回文子串模板---Manacher算法。时间复杂度O(N)
- 最长回文子串算法-- Manacher算法--O(n)
- manacher算法计算最长回文子串
- 串子串Manacher算法: O(n)时间求字符串的最长回文子串
- 最长回文子串——Manacher 算法
- 最长回文子串manacher算法
- 【最长回文子串】HDU3068最长回文【Manacher算法】
- 编程之法:关于最长回文子串 | Manacher 算法详解
- 最长回文子串——Manacher 算法
- LeetCode 5 最长回文子串 Manacher算法
- Manacher算法--O(n)最长回文子串算法
- 最长回文子串——Manacher 算法
- 最长回文子串——Manacher 算法
- Manacher 算法详解:O(n) 复杂度求最长回文子串
- 51nod--1089 最长回文子串V2(Manacher 算法)
- 【模板】Manacher 算法 最长回文子串
- POJ 3974 HDU 3068 最长回文子串 Manacher 算法
- 最长回文子串(Longest Palindrome Substring)--Manacher算法O(n)