1089 最长回文子串

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 
 
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。
输入一个字符串Str,输出Str里最长回文子串的长度。
 
Input
输入Str(Str的长度 <= 100000)
Output
输出最长回文子串的长度L。
Input示例
daabaac
Output示例
5

马拉车算法的模板题
也算是又进一步理解马拉车算法了.

 1 //马拉车算法
 2 #include <bits/stdc++.h>
 3 #define N 1000000
 4 using namespace std;
 5 
 6 int resLen;
 7 int p[N];
 8 int Manacher(string s) {
 9     // Insert '#'
10     string t = "$#";
11     for (int i = 0; i < s.length(); ++i) {
12         t += s[i];
13         t += "#";
14     }
15     // Process t
16     int mx = 0, id = 0, resLen = 0, resCenter = 0;
17     for (int i = 1; i < t.length(); ++i) {
18         //将重复找过的子串直接赋值,多的部分再自行分析
19         p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
20         //多的部分自行分析
21         while (t[i + p[i]] == t[i - p[i]]) 
22             ++p[i];
23         
24         //更新最长到达右边的位置并且记录当前位置
25         if (mx < i + p[i]) {
26             mx = i + p[i];
27             id = i;
28         }
29         //更新最长子串长度,以及半径长和当前位置
30         if (resLen < p[i]) {
31             resLen = p[i];
32             // resCenter = i;
33         }
34     }
35     // return s.substr((resCenter - resLen) / 2, resLen - 1);
36     return resLen - 1;
37 }
38 
39 string s;
40 int main() {
41     cin>>s;
42     cout<<Manacher(s)<<endl;
43     return 0;
44 }
原文地址:https://www.cnblogs.com/zllwxm123/p/9866036.html