1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#pragma comment(linker, "/STACK:10240000")
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
typedef long long ll;
typedef pair<int, int> pii;
#ifndef ONLINE_JUDGE
namespace Debug {
void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
/* -------------------------------------------------------------------------------- */
const int maxn = 3e5 + ;
/** 求字符串每个位置的最大回文半径,在字符串中找最长回文子串 **/
struct Manacher {
int p[maxn];/** 回文半径 **/
char s[maxn];
void init(char str[]) {
strcpy(s, str);
int n = strlen(s);
s[n * + ] = ;
for (int i = n * ; i; i -= ) {
s[i] = '#';
s[i - ] = s[i / - ];
}
s[] = '#';
}
/** 求每个点的最大回文半径 **/
void work() {
int r = , id = ;
p[] = ;
for (int i = ; s[i]; i ++) {
p[i] = i <= r? min(r - i + , p[ * id - i]) : ;
if (p[i] >= r - i + ) {
r = (id = i) + p[i] - ;
while ( * i - r - >= && s[r + ] == s[ * i - r - ]) {
r ++;
p[i] ++;
}
}
}
}
/** 求最长回文串的长度 **/
int solve() {
work();
int ans = ;
for (int i = ; s[i]; i ++) {
ans = max(ans, p[i] - );
}
return ans;
}
};
Manacher solver;
char s[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (~scanf("%s", s)) {
solver.init(s);
printf("%d\n", solver.solve());
}
return ;
}
|